0

I want to delete duplicates values in array. For example: array[1,5,6,1,3,5,9] I want to have array[6,3,9].
I have written this, but I am having troubles:

#include<stdio.h>
main() {
    int array[50], i, j, k=0, c=0, array2[50], n;
    printf("Enter array dimension: "); scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("array[%d]= ", i); scanf("%d", &array[i]);
    }
    for (i = 0; i < n; ) {
        for (j = i + 1; j < n; j++) {
            if (array[i] == array[j])
                i++;
            else {
                array2[k++] = array[i];
                c++;
            }
        }
    }
    for (k = 0; k < c; k++) {
        printf("%d  ", array2[k]);
    }
    system("pause");
}
5
  • 2
    You are not incrementing i in the first for loop. If array[i] == array[j] doesn't happen, i will stay the same forever. Also, k and c seem to have the same value, get rid of one of them. Is the range of numbers limited? Do you have any limits on performance (big O), or is O(n*n) acceptable? Commented May 26, 2014 at 13:12
  • I suppose it would be a small array, 10 - 15 elements. I know, I am getting infinity loop. How can I change that? THanks Commented May 26, 2014 at 13:16
  • Your loop never terminates because, as I've written above, you don't increment i in all cases. Try to debug (step by step, using your favorite debugger) the case when input array is [1,2,3]. Commented May 26, 2014 at 13:17
  • 1
    Where are people learning to write main()?? Seriously, main without arguments has been deprecated/obsolete/bad practice since sometime in the 90s. Are people still using early K&R, or reading C++ books, or what? This is 2014. In C, the main function either takes two argument, or void. It is not a variadic function! Commented May 26, 2014 at 13:19
  • I am student, and I have started to learn C few mounths ago. So, if u can help me, please, help me. Commented May 26, 2014 at 13:22

3 Answers 3

3

You should start by describing your problem in pseudo code, and breaking it into smaller pieces.

Start from scratch by deleting all these redundant variables, and try to implement the following algorithm:

for each element in inputArray
   if not elementIsDuplicate(element)
      add to outputArray

That's a single for loop. The elementIsDuplicate is separate function.

Now try to implement the function elementIsDuplicate. This function also contains a single loop, takes input parameters (int* array, int n, int currentIdx) and returns 1 or 0 indicating whether the element at currentIdx occurs anywhere else in the array.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok, but how can i check if an element is duplicated? I have tried with two loops, the inner loop is checking if numbers are same as the number in outer loop. If it is true, i want to skip that number, and check other. If the number in outer loop is not the same as the numbers in inner loop, I want to add it to output array. That is the logic of this problem. THanks
It would be simpler to have this logic inside the separate function (elementIsDuplicate), which would only check if a single index is repeated elsewhere. For each element in array, if the index is not equal to current index, check if element is equal to element at current index.
That seems like the right way to go, all you need to do is send each index separately in the function and then check the whole matrix if it's a duplicate or not.
1
#include<stdio.h>

int main(){
    int array[50], i, j, k=0, c, n, array2[50] = {0};
    printf("Enter array dimension: "); scanf("%d", &n);
    for (i = 0; i < n; ++i){
        int num, dup = 0;
        printf("array[%d]= ", i); scanf("%d", &num);
        for(j = 0; j < k; ++j){
            if(array[j] == num){
                array2[j] = dup = 1;
                break;
            }
        }
        if(!dup){
            array[k++] = num;
        }
    }

    for (c=i=0; i < k; ++i){
        if(!array2[i])
            printf("%d ", array[c++] = array[i]);
    }
    printf("\n");
/*
    for(i=0;i<c;++i)
        printf("%d ", array[i]);
    printf("\n");
*/
    system("pause");
    return 0;
}

1 Comment

array[c++] = array[i] inside printf, now that's a design choice OP will really need to explain to his teacher. :)
0
#include<stdio.h>
main()
{
int n, a[50], b[50], count = 0, c, d;

printf("Enter number of elements in array\n");
scanf("%d",&n);

printf("Enter %d integers\n", n);
for(c=0;c<n;c++)
    scanf("%d",&a[c]);       //enter array elements

for(c=0;c<n;c++)
{
    for(d=0;d<count;d++)
    {
        if(a[c]==b[d])
            break;
    }
    if(d==count)
    {
        b[count] = a[c];
        count++;
    }
}
printf("count is: %d\n",count);
printf("Array obtained after removing duplicate elements\n");
for(c=0;c<count;c++)
    printf("%d\n",b[c]);
return 0;
}

This will remove multiple duplicates from the desired array.. Example: if the input array is: 3 6 5 6 2 8 6 5 9 8 6 ,,then the output array will be: 3 6 5 2 8 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.