C Program to Count Number of Duplicate Elements in Array

Here is the C program to count duplicate elements in an array.

Required Knowledge


Algorithm to count duplicate elements in an array
Let inputArray is an integer array having N elements.
  • For every element inputArray[i], where (0<=i<=N-1). Search for it's duplicate element from index i+1 to N-1.
  • If duplicate element found, increment the counter and stop searching further for inputArray[i].

C program to find count of duplicate elements in an array

#include <stdio.h>  
  
int main() {  
    int inputArray[100];  
    int i, j, elementCount, count = 0;  
  
    printf("Enter Number of Elements in Array\n");
    scanf("%d", &elementCount);
    printf("Enter %d numbers\n", elementCount);
    
    /* Read array elements */
    for(i = 0; i < elementCount; i++){
        scanf("%d", &inputArray[i]);
    }
  
    for(i = 0; i < elementCount ; i++) {  
        for(j = i+1; j < elementCount; j++) {    
            if(inputArray[i]==inputArray[j]) {
            /* One Duplicate Element Found */  
                count++;  
                break;  
            }  
        }  
    }  
  
    printf("Duplicate Element Count : %d\n", count);  
    return 0;  
}
Output
Enter Number of Elements in Array
8
Enter 8 numbers
1 2 3 4 1 2 3 4
Duplicate Element Count : 4

Related Topics
C program to delete duplicate elements from an array
C program to delete duplicate elements from a sorted array
C Program to find count of each element of an array
C Program to print unique elements of an unsorted array
C Program to search an element in an array
C program to reverse an array
C Program to sort elements of an array in decreasing order
C Program to find count of each element of an array
Program to Merge One Sorted Array into Another Sorted Array
List of all array programs