DEV Community

loading...

Discussion on: Daily Challenge #273 - Remove Duplicates

Collapse
saviourcode profile image
Sourabh Choure

In C with O(n2):

#include <stdio.h>

int solve(int* nums,int* newnums, int numsSize){
    if(numsSize==0){
        return 0;
    }

    int count = 0;

    for(int i=0;i<numsSize;i++){
        int flag = 0;
        for(int j=0;j<count;j++){
            if(nums[i]==newnums[j]){
                flag = 1;
                break;
            }
        }
        if(flag == 0){
            newnums[count++] = nums[i];
        }
    }

    return count;
}

int main(void)
{
    int ar[] = {1,2,1,2,1,1,3};
    int n = sizeof(ar)/sizeof(ar[0]);
    int newar[n];
    int len = solve(ar,newar,n);

    for(int i=0;i<len;i++){
        printf("%d ",newar[i]);
    }
    printf("\n");
    return 0;
}
Collapse
lautarolobo profile image
Lautaro Lobo

Cool!

Collapse
saviourcode profile image
Sourabh Choure

Thanks, I am new to Programming.