Given an array, find the number of elements which occurs only once.
For Example:
If the given array is
1 1 2 3 4 2
The numbers which occurs only once are 3 & 4.
So, the count of such numbers is 2.
Implementation:
For Example:
If the given array is
1 1 2 3 4 2
The numbers which occurs only once are 3 & 4.
So, the count of such numbers is 2.
Implementation:
#include <stdio.h> #include <stdlib.h> #define MAX 1000000 int main(void) { int *a,i,n,hash[MAX],flag=1,count=0; /* Get the number of elements of the array */ scanf("%d",&n); /* Dynamically allocate the memory block for n integers and store the starting address of the block in a */ a=(int*)malloc(sizeof(int)*n); /* Initialize the hash array with 0*/ memset(hash,0,sizeof(hash)); /* Get the elements of the array thereby increment the count of each of the element */ for(i=0;i<n;i++) { scanf("%d",(a+i)); hash[*(a+i)]++; } for(i=0;i<n;i++) { /* If the count of the each element in the array is 1 */ /* increment the variable count */ /* set the flag to zero */ if(hash[*(a+i)]==1) { //printf("%d ",*(a+i)); //hash[*(a+i)]=0; count++; //flag=0; } } /* If the flag is not set to zero, then there are no such elements in the list*/ //if(flag) printf("%d",count); //printf("no such element found"); return 0; }