In: Computer Science
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
If you have any queries please comment in the comments section I will surely help you out and if you found this solution to be helpful kindly upvote.
Solution :
Code :
#include <stdio.h>
// main function
int main()
{
// declare an array
int arr[1000];
// declare size of the array and counter to count the number of
duplicate elements in the array
int n, c = 0;
// input size of the array
printf("Enter the number of elements to be stored in the array:
");
scanf("%d", &n);
// input array elements
printf("Input 3 elements in the arrangement:\n");
for(int i=0; i<n; i++)
{
printf("element [%d]: ",i);
scanf("%d", &arr[i]);
}
// find the number of duplicate elements in the array
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
// if the ith and jth index have same element then a duplicate is
found
if(arr[i] == arr[j])
{
// increment counter
c++;
break;
}
}
}
printf("\nThe total number of duplicate elements found in the array is: %d", c);
return 0;
}
Output :