C Program Count Number of Duplicate Elements in An Array

In this tutorial, i am going to show you how to count the number of duplicate elements in an array with the help of standard method and function in c programs.

All C Programs Count Number of Duplicate Elements in An Array

  • C Program Count Number of Duplicate Elements in An Array using Standard Method
  • C Program Count Number of Duplicate Elements in An Array using Function

C Program Count Number of Duplicate Elements in An Array using Standard Method

#include <stdio.h>
int main()
{
    int a[10000],b[10000],i,j,n,c=0 ;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
  for(i=0; i<n; i++)
    {
         if(a[i]!=-1)
		{
		    for(j=i+1; j<n; j++)
     
            {
        	   if(a[i]==a[j])
        	    {
			       c++;
			       a[j]=-1;
		       }
	       }
 		}
         
   
          
    }
     
            printf("duplicate numbers in the  array: %d",c);
 
         
      
    return 0;
}

The result of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 1 2 5 4 4
duplicate numbers in the  array: 1

C Program Count Number of Duplicate Elements in An Array using Function

#include <stdio.h>
 
count(int *a,int n)
{ 
    int i,c=0,j;
    for(i=0; i<n; i++)
    {
         if(a[i]!=-1)
		{
		    for(j=i+1; j<n; j++)
     
            {
        	   if(a[i]==a[j])
        	    {
			       c++;
			       a[j]=-1;
		        }
	       }
 		}
         
   
          
    }
    return c;
     
 }
 
 
int main()
{
    int a[10000],b[10000],i,n,c;
   
    printf("Enter size of the array : ");
    scanf("%d", &n);
 
    printf("Enter elements in array : ");
    for(i=0; i<n; i++)
    {
        scanf("%d",&a[i]);
    }
    
    c=count(a,n);
    
	printf("duplicate numbers in the  array: %d",c);
 
     
	return 0;
}

The result of the above c program; as follows:

Enter size of the array : 5
Enter elements in array : 11 11 22 22 33
duplicate numbers in the  array: 2

More C Programming Tutorials

Leave a Comment