Thursday, 8 October 2015

get used to PROGRAMMING: Number of Duplicate elements in an Array



Lets see the solution to find Number of Duplicate elements in an Array,

#include <stdio.h>
long long int cmpfunc (const void * a, const void * b)
{
   return ( *(long long int*)a - *(long long int*)b );
}
int main()
{
    long long int size,count=0;

    scanf("%lld",&size);
    int array[size];
    for(int i=0;i<size;i++)
    scanf("%lld",&array[i]);    
    qsort(array, size, sizeof(long long int), cmpfunc);
    for(int i=0;i<size;i++){
      if(array[i]==array[i+1])
            count++;
    }
    printf("%lld\n",count);
    return 0;
}

Output:

No comments:

Post a Comment