C Programming Code Examples C > Arrays and Matrices Code Examples program count total duplicate elements in an array program count total duplicate elements in an array Write a C program to read elements in an array from user and count total number of duplicate elements in array. C program to find all duplicate elements in an unsorted array. How to list all duplicate elements in an unsorted array using loop in C programming. Read elements in an array, say array contains all array elements. Initialize another variable to store duplicate count, say count = 0. To count total duplicate elements in a given array we need two loops. Run an outer loop loop from 0 to N (where N is size of the array). The loop structure must look like for(x=0; x<N; x++). This loop is used to select each element of array and check next subsequent elements for duplicates elements using another nested loop. Run another inner loop to find first duplicate of current element i.e. array[x]. Run an inner loop from x + 1 to N, the loop structure must look like for(y=x+1; y<N; y++). Now, why run a loop from x + 1. Because we need to search for duplicate elements in next subsequent from current element and array[x] represents the current element. Inside the inner loop check for duplicate element. If a duplicate element is found then increment duplicate count. Which is if(array[x] == array[y]) then, count++. Also terminate inner loop if a duplicate element is found. /* * C program to count total number of duplicate elements in an array */ #include <stdio.h> int main() { int array[100]; int x, y, n, count = 0; /* Input size of the array */ printf("Enter size of the array : "); scanf("%d", &n); /* Input elements in the array */ printf("Enter elements in array : "); for(x=0; x<n; x++) { scanf("%d", &array[x]); } /* * Find all duplicate elements in array */ for(x=0; x<n; x++) { for(y=x+1; y<n; y++) { /* If duplicate found then increment count by 1 */ if(array[x] == array[y]) { count++; break; } } } printf("\nTotal number of duplicate elements found in array = %d", count); return 0; }