0

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?

5
  • 1
    Please, indent the code. Commented Jul 18, 2015 at 8:41
  • Did you try to debug the code using a debugger, stepping throught the code line by line seeing where it goes and how the relevant variables are changed? Commented Jul 18, 2015 at 8:47
  • What happens if size >= 100? Commented Jul 18, 2015 at 8:50
  • If the number is greater than 100 it would be out of range because my array has 100 slots only, right? Can this be fixed by declaring the array after the user has input the size? I mean I know it does work since I just tried it but is this okay to do? Commented Jul 18, 2015 at 9:32
  • @mhenkes92 Yes, it's OK to do so. It's called "Variable Length Array". It's available in C99 onwards and use it with caution as inputting too large a number for array may fail. Commented Jul 18, 2015 at 10:05

5 Answers 5

1

You have to check if the number is already in the duplicate array before adding it to the duplicate array.

This check

if( array[j]==array[y] && y!=j )

prevents against comparing the number at the same index but not against comparing a same value that's at different index.

So it should be

if( array[j]==array[y] && y!=j && !InDup(dupArray, array[j], x) )
{
 ...
}

and InDup() would check if the element is in the array:

int InDup(int *a,int num, int x)
{
size_t i;

for (i=0; i<x; i++)
if(a[i] == num) return 1;

return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

0

Omg. Thanks! I knew I had to find out if my dupArray already contains a duplicate number but what I was doing to solve the problem did not work. Your logic makes perfect sense. Here's my quickly adjusted code (now working):

#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, exists = 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++){
exists = 0; 
for(m=0; m<=x; m++){
    if(array[j]==dupArray[m]){
    exists = 1;
    }   
}   
if(array[j]==array[y]&&y!=j && !exists)
{

    dupArray[x] = array[j];

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


return 0;
}

Now I'm going to make it look nice and use functions as a programmer should. :) Thanks again!

Comments

0

A simple and clean implementation of the problem

#include <stdio.h>

int exists(int arr[], int size, int num) {
    int i;
    for (i = 0; i < size; ++i) if (arr[i] == num) return 1;
    return 0;
}

int main(void) {
    int size, i, len = 0;
    scanf("%d", &size);
    int array[size], dups[size];
    for (i = 0; i < size; ++i) {
        scanf("%d", &array[i]);
        if (exists(array, i, array[i]) && !exists(dups, len, array[i]))
            printf("%d ", dups[len++] = array[i]);
    }
    return 0;
}

Input

10
5 5 6 3 6 7 9 0 0 4

5
5 1 5 6 5

Output

5 6 0

5

See DEMO.

Comments

0

You can try this code:

 #include <stdio.h>

int checkDuplicateInArray(int *s, int n){
    int number = 3;
    int found = 0, i = 0;

    while(i<n){
        if(s[i] == number){
            if(found == 0){
                printf("There was an\t\t%d\tFound at position\t%d\n", s[i], i+1);
                found++;
            }else{
                printf("There was another\t%d\tFound at position\t%d\n", s[i], i+1);
            }
        }
        i++;
    }

    if(found == 0){
        printf("There was no match Found\n");
        return 1;
    }
    return 0;
}

int main(void){
    int array[] = {10,2,65,3,5,19,7,8,3,10,55,12,99,3,17};
    int length = sizeof array / sizeof array[0];

    checkDuplicateInArray(array,length);

    return 0;
}

Output:

There was an      3   Found at position   4
There was another   3   Found at position   9
There was another   3   Found at position   14

This should be enough to give you an Idea.

Comments

0

First of all, your app might crash, if the user enters "101" when asked how many numbers he want to compare. That's because your reserve only space for 100 numbers in memory but you allow the user to enter as many numbers as he wants. You fix that by either limiting the size:

int size;
int array[100];
while (true) {
  printf("How many numbers do you want to compare? ");
  scanf("%d", &size);

  if (size <= 100) break;
  printf("At most 100 numbers are allowed!\n");
}

This will keep asking, until the user enters a numbers smaller or equal to 100 (or until he hits CTRL+C to terminate the program) or you allocate memory dynamically:

int size;
int * array = NULL;
while (true) {
  printf("How many numbers do you want to compare? ");
  scanf("%d", &size);

  if (size > 0) break;
  printf("You must enter a value > 0!\n");    
}
// calloc(x,y) => reserve memory for x elements,
//                each y bytes in size.
array = calloc(size, sizeof(int));

Then the most efficient way to check for duplicates in an array of numbers is to actually sort that array (or if you need to keep the original order, to sort a copy of it) and then duplicates will be directly next to each other. E.g. input is 3 5 7 5 9 2 7 4 8 3 and now you sort it, it will be 2 3 3 4 5 5 7 7 8 9 and now it's very easy to spot the duplicates. Finding duplicates in a sorted array is like that:

int i;
int lastValue = array[0];
// Of course the statement below is "a lie",
// I just need lastValueReported to be different to lastValue
int lastValueReported = lastValue - 1; 
for (i = 1; i < size; i++) {
  int currentValue = array[i];
  if (currentValue == lastValue) {
    // Wow, we have a duplicate!
    // But have we reported it already?
    if (currentValue != lastValueReported) {
      printf("duplicate: %d\n", currentValue);
      lastValueReported = currentValue;
    }
  }
  lastValue = currentValue; 
}

Now that was easy, wasn't it? This leaves you only with one last problem: How to sort an array of ints. Fortunately sorting is a very well known problem with a countless number of possible solutions and I leave this exercise to the reader. I you think about it, you may even come up with a simple sort scheme like selection sort or bubble sort all by yourself. These two are not very fast but sufficient for your use case.

Comments

Your Answer

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