Count total number of duplicate elements in an array
C program to count total number of duplicate elements in an array.
Program
#include<stdio.h>
#include<conio.h>
#define MAX 50
void read_array(int a[MAX], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("Element %d\t:",i+1);
scanf("%d",&a[i]);
}
}
int dup_ele(int a[MAX], int freq[MAX], int n)
{
int i, j, count, count1;
for(i=0;i<n;i++)
{
freq[i] = -1;
}
for(i=0;i<n;i++)
{
count = 1;
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
count++;
freq[j] = 0;;
}
}
if(freq[i] != 0)
{
freq[i] = count;
}
}
count1 = 0;
for(i=0;i<n;i++)
{
if(freq[i] > 1)
{
count1++;
}
}
return(count1);
}
void main()
{
int arr[MAX], freq[MAX];
int i, n, count;
printf("Enter the number of elements in the array\t:");
scanf("%d",&n);
printf("Enter the elements of the array\n");
read_array(arr, n);
count = dup_ele(arr, freq, n);
printf("\nTotal number of duplicate elements is\t:%d",count);
getch();
}
Output
Enter the number of elements in the array :7
Enter the elements of the array
Element 1 :12
Element 2 :21
Element 3 :12
Element 4 :36
Element 5 :54
Element 6 :36
Element 7 :54
Total number of duplicate elements is :3