user14048924
user14048924

Reputation:

How to print the number of duplicate elements in array in C

any help would be greatly appreciated. In this code below, i understand that we need to use "j=i+1" because the same element in the original array and duplicate array should not get recorded as a duplicate element. But, lets say during the 2nd loop, when i=2,so j=3,then the loop will run from the 3rd element right? and how will it compare the 2nd element in the array with the 1 element in the duplicate? will it not miss the first element in the duplicate because it is j+1? please explain this to me , I am a beginner in programming. and also, why is count++ used ? can someone explain this to me ...?

#include <stdio.h>

#define MAX_SIZE 100  // Maximum array size

int main()
{
    int arr[MAX_SIZE];
    int i, j, size, count = 0;

    /* Input size of array */
    printf("Enter size of the array : ");
    scanf("%d", &size);

    /* Input elements in array */
    printf("Enter elements in array : ");
    for(i=0; i<size; i++)
    {
        scanf("%d", &arr[i]);
    }

    /*
     * Find all duplicate elements in array
     */
    for(i=0; i<size; i++)
    {
        for(**j=i+1**; j<size; j++)
        {
            /* If duplicate found then increment count by 1 */
            if(arr[i] == arr[j])
            {
                **count++;**
                break;
            }
        }
    }

    printf("\nTotal number of duplicate elements found in array = %d", count);

    return 0;
}

Upvotes: 0

Views: 973

Answers (3)

Vijay Pratap Singh
Vijay Pratap Singh

Reputation: 53

If you do not want to sort the array and don't want to use any hash map. Here is the solution :

int dup(int a[],int n){
int count=0;
int i=0;
int j=0;
for(i=0;i<n;i++){
    for(j=0;j<n;j++){
        if( a[i]==a[j] && j!=i){
            if(j>i){
                count++;
            }   
            break;
        }
    }
}
return count;

}

Upvotes: 0

Eric Postpischil
Eric Postpischil

Reputation: 222467

There are multiple ways to think about this. First, consider the code you have:

for(i=0; i<size; i++)
{
    for(j=i+1; j<size; j++)
    {
        /* If duplicate found then increment count by 1 */
        if(arr[i] == arr[j])
        {
            count++;
            break;
        }
    }
}

The statement count++; increments the value stored in count. The break ends the inner loop (at which point the outer loop continues and may start a different execution of the inner loop).

You can try this code on simple examples, such as [1, 2, 3], [1, 1, 2], and [1, 2, 1]. What results do you get?

Next, try it on [1, 1, 1] or [1, 2, 1, 3, 1]. What results do you get? What result do you want?

Can you describe a characteristic of the situations in which the code produces a result different from what you want?

Once you have done that, can you think of a solution? What is the code doing that you want it not to do?

Upvotes: 0

Tom&#225;š Opichal
Tom&#225;š Opichal

Reputation: 31

To answer your question (hope I understand it well):

  • When i == 0, you compare first element with all other elements - either you find some duplicates or you don't.
  • When i == 1, you compare second element with all elements except the first one - you don't need to compare with first because you've already done it in previous step.
  • Etc..

Upvotes: 1

Related Questions