Signup/Sign In

C Program To Find the frequency of each element in the array

We are given an unsorted array and our task is to find the frequency of each element in the array. But before moving forward, if you are not familiar with the concept of the array then, do check the article on Arrays in C.

Input: Enter the arrray elements: 5 4 6 7 3 4 1

Output:

The frequency of all the elements in the array are:

5 occurs 1 time

4 occurs 2 times

6 occurs 1 time

7 occurs 1 time

3 occurs 1 time

1 occurs 1 time

Program 1: Find the Frequency of Each Element in an Array

In this method, consider two arrays and two for loops. The first for loop will hold an element in the array and the second for loop will look for duplicate elements. If duplicate elements are found then, it will increment their frequencies and store them in another array. Also, a count variable is used to check if the element is previously counted or not.

Let us take a look at the algorithm for a better understanding.

Algorithm

  1. Start
  2. Declare an array
  3. Initialize the array
  4. Declare another array to store the frequency of the elements.
  5. The size of both the arrays should be the same.
  6. Declare two for loops
  7. First for loop is used to select the array element and the second for loop is used to find the first duplicate element of the currently selected array element by the outer loop.
  8. The first loop should run from 0 to n-1.
  9. Inside the first for loop declare a count variable and initialize it to 1.
  10. This count variable will count the total frequency of the currently selected array element.
  11. Run an inner loop to count the total duplicates of the currently selected array element. This loop should run from i+1 to n.
  12. Inside the inner loop, if a duplicate element is found, increment the frequency count of the current array element.
  13. The above step says: if (a[i] == a[j]) then count++.
  14. After counting all the duplicate elements store the total duplicate count of the current element in the frequency array.
  15. The above step says: freq[i] = count.
  16. Finally, print the array element to get the frequency of each element.

In the below program, firstly we declare two arrays. The first array will store the elements of the original array and the second array will store the frequency of the second array. Now, calculate the frequency of each element using two for loops. Then, display the result.

#include <stdio.h>
int main()
{
	int  i, j, Count, n;	
	printf("\n Enter the number of elements in an array  :   ");
	scanf("%d", &n);               //Declare size array	
    int a[n], Freq[n];            //Declare two arrays
	printf("\n Enter the elements of an Array  :  ");
	for (i = 0; i < n; i++)       //Initialize both the arrays
	{
    	    scanf("%d", &a[i]);
    	    Freq[i] = -1;         /* Initially initialize frequencies to -1 */
   	}      
   //Count the frequency of each element
	for (i = 0; i < n; i++)
	{
		Count = 1;
		for(j = i + 1; j < n; j++)
		{
    		if(a[i] == a[j])    //Check for duplicate elements
    		{
    			Count++;
    			Freq[j] = 0;    /* Make sure not to count frequency of same element again */
    		}
    	}
    	if(Freq[i] != 0)        /* If frequency of current element is not counted */
    	{
    		Freq[i] = Count;
		}
	}
    /* Print frequency of each element*/
 	printf("\n The Frequency of the elements in this Array is : \n");
 	for (i = 0; i < n; i++)
  	{
  		if(Freq[i] != 0)        
  		{
  			printf("%d Occurs %d Times \n", a[i], Freq[i]);
		}		
  	}	     
 	return 0;
}


Enter the number of elements in an array: 7
Enter the elements of an Array : 6 8 6 5 4 6 9
The frequency of the elements in this Array is:
6 Occurs 3 Times
8 Occurs 1 Times
5 Occurs 1 Times
4 Occurs 1 Times
9 Occurs 1 Times

Program 2: Find the Frequency of Each Element in an Array

In this method, consider two arrays and two for loops. The first for loop will hold an element in the array and the second for loop will look for duplicate elements. If duplicate elements are found then, it will increment their frequencies and store them in another array. Also, a count variable is used to check if the element is previously counted or not.

Let us take a look at the algorithm for a better understanding.

Algorithm

  1. Start
  2. Declare an array
  3. Initialize the array
  4. Declare another array to store the frequency of the elements.
  5. The size of both the arrays should be the same.
  6. Call a function that will count the frequency of each element in an array.
  7. Declare two for loops. First for loop is used to select the array element and the second for loop is used to find the first duplicate element of the currently selected array element by the outer loop.
  8. The first loop should run from 0 to n-1.
  9. Inside the first for loop declare a count variable and initialize it to 1.
  10. This count variable will count the total frequency of the currently selected array element.
  11. Run an inner loop to count the total duplicates of the currently selected array element. This loop should run from i+1 to n.
  12. Inside the inner loop, if a duplicate element is found, increment the frequency count of the current array element.
  13. The above step says: if (a[i] == a[j]) then count++.
  14. After counting all the duplicate elements store the total duplicate count of the current element in the frequency array.
  15. The above step says: freq[i] = count.
  16. Finally, print the array element to get the frequency of each element.

In the below program, firstly we declare two arrays and then call a function. The first array will store the elements of the original array and the second array will store the frequency of the second array. Now, calculate the frequency of each element using two for loops. Then, display the result.

#include <stdio.h>
//Count the frequency of each element
 void countFreq(int a[10],int n,int Freq[10])    //Function Definition
 {
     for (int i = 0; i < n; i++)
	{
		int Count = 1;
		for(int j = i + 1; j < n; j++)
		{
    		if(a[i] == a[j])    //Check for duplicate elements
    		{
    			Count++;
    			Freq[j] = 0;    /* Make sure not to count frequency of same element again */
    		}
    	}
    	if(Freq[i] != 0)        /* If frequency of current element is not counted */
    	{
    		Freq[i] = Count;
		}
	}
    /* Print frequency of each element*/
 	printf("\n The Frequency of the elements in this Array is : \n");
 	for (int i = 0; i < n; i++)
  	{
  		if(Freq[i] != 0)        
  		{
  			printf("%d Occurs %d Times \n", a[i], Freq[i]);
		}		
  	}
 }
int main()
{
	int  i, j, n;	
	printf("\n Enter the number of elements in an array  :   ");
	scanf("%d", &n);               //Declare size array	
    int a[10], Freq[10];            //Declare two arrays
	printf("\n Enter the elements of an Array  :  ");
	for (i = 0; i < n; i++)       //Initialize both the arrays
	{
    	    scanf("%d", &a[i]);
    	    Freq[i] = -1;         /* Initially initialize frequencies to -1 */
   	}     
    countFreq(a,n,Freq);   //Function call		     
 	return 0;
}


Enter the number of elements in an array : 7
Enter the elements of an Array : 1 4 8 5 6 4 9
The Frequency of the elements in this Array is :
1 Occurs 1 Times
4 Occurs 2 Times
8 Occurs 1 Times
5 Occurs 1 Times
6 Occurs 1 Times
9 Occurs 1 Times



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight