C PROGRAM TO FIND COUNT OF DUPLICATE ELEMENTS IN AN ARRAY
In this post we will see a simple C program to find the count of duplicate elements in an Array.
In below program we will get the count of elements that are duplicated i.e. elements that are occurred more than once in an array.
Suppose the elements of array are : 10,20,30,10,10
Here the element 10 is repeated 3 times in array, it means it is duplicated 2 more times and remaining elements occurred only once. Hence its output will be 2.
Program Algorithm :
Step 1: Read size of array and store it in nStep 2: If n (i.e size of array) is greater than MAX, print message and exit programStep 3: Read elements of array and store in arr[MAX] , here MAX is a global variable whose valueis 25 in the program belowStep 4: Initialize count = 0, to avoid garbage valuesStep 5: Set i = 0Step 6: Set j = i+1Step 7: Check for duplicate, if arr[i] = arr[j] then increment count by 1 i.e. count = count + 1Step 8: Increment j by 1 i.e. j = j + 1 and repeat Step 7 till j<nStep 9: Increment i by 1 i.e. i = i + 1 and repeat Step 6-7 till i<nStep 10: Print the count and exit program
Sample output :
Sample output text :
*** www.ProgrammingPosts.com ***>>> C program to count duplicate elements in an array <<<Enter the size of array: 5Enter the 5 elements of array:arr:10arr:20arr:30arr:10arr:20Count of duplicate elements in an array : 2