Display duplicates in array in C

I am currently programming in C and now I am working on a program that stores duplicates of a user input in an array and outputs them. First, the program seemed easy to me but then I realized there are some issues that I just haven’t figured out for a week now despite endless Google searches and youtube tutorials. Most tutorials show you how to delete duplicates in an array or similar. It seems like displaying them should be very similar to deleting duplicates but the way I want them to be displayed seems impossible to me although I’m convinced the solution is super simple.
So here is the basic logic. Two for loops and an if that compares the arrays:

#include<stdio.h>

int main(void){

int dupArray[100], newArray[100], array[100], size = 0, number=0, i=0, j=0,        
x=0, y = 0, m = 0, n = 0;

printf("How many numbers do you want to compare? ");
scanf("%d", &size);

for(i = 0; i < size; i++){  
printf("Enter number %d", i+1);
printf("/%d: ", size);
scanf("%d", &array[i]);
}
for(y=0; y<size; y++){
for(j=0; j<size; j++){      
if(array[j]==array[y]&&y!=j)
{

dupArray[x] = array[j];

printf("duplicate: %d", dupArray[x]);
printf("n");
x++;
}
}
}


return 0;
}

The program works fine for double numbers. For instance, a array size of 10 with user input 5 5 6 3 6 7 9 0 0 4 results in
duplicate: 5
duplicate: 5
duplicate: 6
duplicate: 6
duplicate: 0
duplicate: 0
However, as soon as more than 2 of the same numbers are being entered, the program outputs a lot of the same numbers. For example:
array size 5. user input: 5 1 5 6 5
duplicate: 5
duplicate: 5
duplicate: 5
duplicate: 5
duplicate: 5
duplicate: 5
What do I have to do to make the numbers – regardless of how often the same numbers are entered – output only once?


Source: c#

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.