Count frequency of element in array

// This program will help you to generate the frequency of all element present in array


#include<stdio.h>

int main()

  int  *arr,i,j,k,n,temp,f=0;

  printf("Enter the numer of array \n  ");
   scanf("%d",&n);

  arr=(int*)malloc(sizeof(int)*n);    // allocating  the space         

  printf("Enter the element now \n");
    for(i=0;i<n;i++)
      scanf("%d",(arr+i));          // feeding the value in array

 // sorting the array

  for(i=0;i<n;i++)
     {
       for(j=i;j<n;j++)
         {
              if(arr[i]>arr[j])
              {
                 temp=arr[i];
                 arr[i]=arr[j];
                 arr[j]=temp;
              }
         }
     }
  for(i=0;i<n;i++)
    {
     if(arr[i]==arr[i+1])
         f++;
    else
      {
        printf("Frequency of %d is %d \n ",arr[i],(f+1));
          f=0;
      }
    }
  return 0;
 }

  


Share on Google Plus

0 comments: