Q:

Write C program count total duplicate elements in an array

0

Write C program count total duplicate elements in an array

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

int main()
{
    int arr[100];
    int i, j, n, count = 0;

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

    //Reading elements of array
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d", &arr[i]);
    }
    //Find all duplicate elements in array
    for(i=0; i<n; i++)
    {
        for(j=i+1; j<n; j++)
        {
        // If duplicate element 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;
}

Result:

Enter size of the array : 5

Enter elements in array : 1

2

3

5

1

Total number of duplicate elements found in array = 1

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now