C program to count the total number of duplicate elements in an array – In this article, we will discuss the numerous methods to count the total number of duplicate elements in an array in C programming.
Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.
The means used in this specific piece are as follows:
- Using Standard Method
- Using Function
An array, as you all know, is a general order or sequence of elements set in any possible manner. Arrays are specifically used to arrange the elements and the storage of those are denoted by pointers.
As you can see in the photo uploaded above, firstly, you need to enter the size of the array concerned.
In this case, the size of the array was finalised at 5.
Then, the elements in the array are added accordingly.
The elements added in this particular array are as follows: 1 1 2 1 2
So, it is clear that out of the 5 elements, 3 elements are duplicate.
Thus, 3 will be printed as the final result.
Hence, the means to count the total number of duplicate elements in an array in C programming are as follows:
Using Standard Method
- Read the entered array size and store the value into the variable n and count initialized to 0.
2) Read the entered elements and store the elements in the array a[] as scanf(“%d”,&a[i]) using for loop for(i=0;i<n;i++).
3) Find the duplicate elements in the array as
for i=0 to i<n
a) If a[i]!=-1 then compare each element with remaining elements of the array. If any element repeated more than one time then increase the count value by 1. Repeat until j<n, using for loop for(j=i+1;j<n;j++).
4) After all iterations of i, print the count value c which represents the number of duplicate elements in the array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
#include <stdio.h> #include <conio.h> int main() { int a[10000],b[10000],i,j,n,c=0 ; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } for(i=0; i<n; i++) { if(a[i]!=-1) { for(j=i+1; j<n; j++) { if(a[i]==a[j]) { c++; a[j]=-1; } } } } printf("duplicate numbers in the array: %d",c); return 0; } |
Output:
1 2 3 |
Enter size of the array: 5 Enter elements in array : 1 1 2 1 2 duplicate numbers in the array: 3 |
Using Function
- The main() function calls the count() function passing array a, the size of the array are arguments to the function.
2) The function count() finds how many elements repeated in the array and returns the c value to main().
for i=0 to i<n
a) If a[i]!=-1
Then compare a[i] with remaining elements using for loop from j=i+1 t0 j<n.
If any element is equal to a[i] then increase the count value by 1. Like this repeat until j<n.
3) After all iterations of i,the count() function returns the c value to main() function then main() prints the c value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
#include <conio.h> count(int *a,int n) { int i,c=0,j; for(i=0; i<n; i++) { if(a[i]!=-1) { for(j=i+1; j<n; j++) { if(a[i]==a[j]) { c++; a[j]=-1; } } } } return c; } int main() { int a[10000],b[10000],i,n,c; printf("Enter size of the array : "); scanf("%d", &n); printf("Enter elements in array : "); for(i=0; i<n; i++) { scanf("%d",&a[i]); } c=count(a,n); printf("duplicate numbers in the array: %d",c); return 0; } |
1 2 3 |
Enter size of the array: 5 Enter elements in array : 1 2 3 1 2 duplicate numbers in the array: 2 |