Print all duplicate elements in array
C program to print all duplicate elements in 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]);
}
}
void print_dup(int a[MAX], int freq[MAX], int n)
{
int i, j, count;
printf("\nDuplicate elements in the array are\t:");
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;
}
}
for(i=0;i<n;i++)
{
if(freq[i] > 1)
{
printf("%d,\t",a[i]);
}
}
}
void main()
{
int arr[MAX], freq[MAX];
int i, n;
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);
print_dup(arr, freq, n);
getch();
}
Output
Enter the number of elements in the array :8
Enter the elements of the array
Element 1 :12
Element 2 :21
Element 3 :23
Element 4 :32
Element 5 :21
Element 6 :32
Element 7 :25
Element 8 :12
Duplicate elements in the array are :12, 21, 32,