How to write a C Program to Count Total Duplicate Elements in an Array. Before going into this article Please refer Array in C article to understand the concept of Array size, index position etc.
C Program to Count Total Duplicate Elements in an Array Example
This program asks the user to enter Array Size, and array elements. Next, it is going to count total number of duplicate elements present in this array using For Loop.
/* C Program to Count Total Duplicate Elements in an Array */ #include <stdio.h> int main() { int arr[10], i, j, Size, Count = 0; printf("\n Please Enter Number of elements in an array : "); scanf("%d", &Size); printf("\n Please Enter %d elements of an Array : ", Size); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); } for (i = 0; i < Size; i++) { for(j = i + 1; j < Size; j++) { if(arr[i] == arr[j]) { Count++; break; } } } printf("\n Total Number of Duplicate Elements in this Array = %d ", Count); return 0; }
OUTPUT
ANALYSIS
In this Program, We declared 1 One Dimensional Arrays arr[] of size 10 and also declared i to iterate the Array elements
Below printf statement asks the User to enter the array arr[] size (Number of elements an Array can hold). And, scanf statement will assign the user entered values to Size variable.
printf("\n Please Enter Number of elements in an array : "); scanf("%d", &Size);
Below For loop will help to iterate each and every cell present in the arr[5] array. Condition inside the for loops (i < Size) will ensure the compiler, not to exceed the array limit.
scanf statement inside the for loop will store the user entered values in every individual array element such as arr[0], arr[1], arr[2], arr[3], arr[4]
for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); }
In the next line, We have one more for loop, it is used to iterate each and every element in an array. If Statement to check for the duplicates
for (i = 0; i < Size; i++) { for(j = i + 1; j < Size; j++) { if(arr[i] == arr[j]) { Count++; break; } } }
From the above screenshot you can observe that, User inserted values are
a[5] = {10, 20, 10, 30, 20}
First For Loop – First Iteration: for(i = 0; i < 5; 0++)
The condition (0 < 5) is True.
Second For Loop – First Iteration: for(j = 0 + 1; 1 < 5; 1++)
The condition (1 < 5) is True. So, it will start executing the statements inside the loop
if(arr[i] == arr[j])
if(10 == 20) – Condition is False
Second For Loop – Second Iteration: for(j = 2; 2 < 5; 2++)
The condition (2 < 5) is True. So, it will start executing the statements inside the loop
if(arr[i] == arr[j])
if(10 == 10) – Condition is True
Count will be incremented to 1
Do the same for remaining iterations.
C Program to Count Total Duplicate Elements in an Array Example 2
This program is same as above but this time we separated the logic using using Functions concept
/* C Program to Count Total Duplicate Elements in an Array */ #include <stdio.h> int Total_Duplicates(int arr[], int Size); int main() { int arr[10], i, Size, Count = 0; printf("\n Please Enter Number of elements in an array : "); scanf("%d", &Size); printf("\n Please Enter %d elements of an Array : ", Size); for (i = 0; i < Size; i++) { scanf("%d", &arr[i]); } Count = Total_Duplicates(arr, Size); printf("\n Total Number of Duplicate Elements in this Array = %d ", Count); return 0; } int Total_Duplicates(int arr[], int Size) { int i, j, Count = 0; for (i = 0; i < Size; i++) { for(j = i + 1; j < Size; j++) { if(arr[i] == arr[j]) { Count++; break; } } } return Count; }
OUTPUT