w3resource

C Exercises: Count a total number of duplicate elements in an array

C Array: Exercise-5 with Solution

Write a program in C to count the total number of duplicate elements in an array.

Pictorial Presentation:

C Exercises: Count a total number of duplicate elements in an array

Sample Solution:

C Code:

#include <stdio.h>

void main()
{
    int arr1[100];
	int arr2[100];
	int arr3[100];
    int n,mm=1,ctr=0;
    int i, j;	
	
	
       printf("\n\nCount total number of duplicate elements in an array:\n");
       printf("---------------------------------------------------------\n");	

       printf("Input the number of elements to be stored in the array :");
       scanf("%d",&n);
   
       printf("Input %d elements in the array :\n",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;
        }		
/*--------------- Prints the array ------------------------------------*/
   for(i=0; i<n; i++)
    {
      if(arr3[i]==2){ctr++;}  
    }
      printf("The total number of duplicate elements found in the array is: %d \n", ctr);
    
	  printf("\n\n");		
}

Sample Output:

Count total number of duplicate elements in an array:                                                         
---------------------------------------------------------                                                     
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                                                                                               
                                                                                                              
Total number of duplicate elements found in the array is : 1

Flowchart:

Flowchart: Count total number of duplicate elements in an array

C Programming Code Editor:

Improve this sample solution and post your code through Disqus.

Previous: Write a program in C to copy the elements one array into another array.
Next: Write a program in C to print all unique elements in an array.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

C Programming: Tips of the Day

C Programming - What is the argument for printf that formats a long?

Put an l (lowercased letter L) directly before the specifier.

unsigned long n;
long m;

printf("%lu %ld", n, m);

Ref : https://bit.ly/3dIwfkP