Count frequency of each array element - C Program

Write a C program to count the frequency of each element from an array.

Solution:

Here is an example to count the total occurrences of any number in the given array. We will make a function (frequency) to check the frequency of each element in the array.

Program

#include<stdio.h>
#include<conio.h>
#define MAX 20
void frequency(int [],int);

int main()
{
     int a[MAX],n,i;
     printf("Enter limit of array: ");
     scanf("%d",&n);
     printf("\nEnter array: ");
     for(i=0;i<n;i++)
     {
          scanf("%d",&a[i]);
     }
     frequency(a,n);
     return(0);
}
void frequency(int a[],int n)
{
     int i,j,k,num,c=0;
     for(i=0;i<n;i++)
     {
          num=a[i];
          c=1;
          for(j=i+1;j<n;j++)
          {
               if(a[j]==num)
               {
                    c++;
                    for(k=j;k<n;k++)
                    {
                         a[k]=a[k+1];
                    }
                    n--;
                    j--;
                }
          }
          printf("\nthe frequency of element %d  is %d",a[i],c);
     }
}


Output:

frequency of element