Starting Vocational Training From 1-May-2024 Get Detail
Write a program in C to count a total number of duplicate elements in
an array.
Test Data :
Input the number of elements to be stored in the array :3
Input 3 elements in the array :
element - 0 : 5
element - 1 : 1
element - 2 : 1
Expected Output :
Total number of duplicate elements found in the array is : 1
#include <stdio.h>
void main()
{
int arr1[100];
int arr2[100];
int arr3[100];
int n,mm=1,ctr=0;
int i,j ;
printf("
Count total number of duplicate elements in an array:
");
printf("---------------------------------------------------------
");
printf("Input the number of elements to be stored in the array :");
scanf("%d",&n);
printf("Input %d elements in the array :
",n);
for(i=0;i<n;i++)
{
printf("element - %d : ",i);
scanf("%d",&arr1[i]);
}
/*----------------- copy in other array ------------------------------------*/
for(i=0;i<n; i++)
{
arr2[i]=arr1[i];
arr3[i]=0;
}
/*------------------- mark the elements are duplicate -------------------------*/
for(i=0;i<n; i++)
{
for(j=0;j<n;j++)
{
if(arr1[i]==arr2[j])
{
arr3[j]=mm;
mm++;
}
}
mm=1;
}
for(i=0; i<n; i++)
{
if(arr3[i]==2){ctr++;}
}
printf("The total number of duplicate elements found in the array is: %d
", ctr);
printf("
");
}