C Program to Count Frequency of each Element in an Array

How to write a C Program to Count Frequency of each Element in an Array?. Before going into this article Please refer to Array in C article to understand the concept of Array size, index position, etc.

C Program to Count Frequency of each Element in an Array Example 1

This program asks the user to enter Array Size and array elements. Next, it is going to find the frequency (total number of times) of each element present in this array.

/* C Program to Count Frequency of each Element in an Array */
 
#include <stdio.h>
 
int main()
{
	int arr[10], FreqArr[10], i, j, Count, Size;
	
	printf("\n Please Enter Number of elements in an array  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array  :  ", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &arr[i]);
    	FreqArr[i] = -1;
   	}     
 
	for (i = 0; i < Size; i++)
	{
		Count = 1;
		for(j = i + 1; j < Size; j++)
		{
    		if(arr[i] == arr[j])
    		{
    			Count++;
    			FreqArr[j] = 0;
    		}
    	}
    	if(FreqArr[i] != 0)
    	{
    		FreqArr[i] = Count;
		}
	}

 	printf("\n Frequency of All the Elements in this Array are : \n");
 	for (i = 0; i < Size; i++)
  	{
  		if(FreqArr[i] != 0)
  		{
  			printf("%d Occurs %d Times \n", arr[i], FreqArr[i]);
		}		
  	}	     
 	return 0;
}
C Program to Count Frequency of each Element in an Array 1

In this C Program to Count Frequency of each Element in an Array, We declared 1 One Dimensional Arrays arr[] of size 10 and also declared i to iterate the Array elements

Below C Programming printf statement asks the User to enter the array arr[] size (Number of elements an Array can hold). And, scanf statement will assign the user entered values to Size variable.

printf("\n Please Enter Number of elements in an array  :   ");
scanf("%d", &Size);

Below For loop will help to iterate each cell present in the arr[5] array. Condition (i < Size) inside the for loop ensures the compiler not to exceed the array limit.

scanf statement inside the for loop will store the user entered values in every individual array element such as arr[0], arr[1], arr[2], arr[3], arr[4]. FreqArr[i] = -1 means we haven’t started counting the frequency of an element yet

for (i = 0; i < Size; i++)
{
    	scanf("%d", &arr[i]);
        FreqArr[i] = -1;
}

In the next line, We have two more for loops to iterate each element in an array. If Statement to check for the duplicates

for (i = 0; i < Size; i++)
{
	Count = 1;
	for(j = i + 1; j < Size; j++)
	{
   		if(arr[i] == arr[j])
   		{
    			Count++;
    			FreqArr[j] = 0;
    		}
    	}
    	if(FreqArr[i] != 0)
    	{
    		FreqArr[i] = Count;
	}
}

From the above screenshot, you can observe that User inserted values for C Program to Count Frequency of each Element in an Array are

a[5] = {10, 20, 20, 10, 50}

First For Loop – First Iteration: for(i = 0; i < 5; 0++)
The condition (0 < 5) is True.
Count = 1

Second For Loop – First Iteration: for(j = 0 + 1; 1 < 5; 1++)
The condition (1 < 5) is True. So, it will start executing the statements inside the loop
if(arr[i] == arr[j])
if(10 == 20) – Condition is False

Second For Loop – Second Iteration: for(j = 2; 2 < 5; 2++)
The condition (2 < 5) is True.
if(arr[i] == arr[j])
if(10 == 20) – Condition is False

Second For Loop – Third Iteration: for(j = 3; 3 < 5; 3++)
The condition (3 < 5) is True.
if(arr[i] == arr[j])
if(10 == 10) – Condition is True
Count++ means Count = 2
FreqArr[j] = 0

Second For Loop – Fourth Iteration: for(j = 4; 4 < 5; 4++)
The condition (4 < 5) is True.
if(arr[i] == arr[j])
if(10 == 50) – Condition is False

Second For Loop – Fifth Iteration: for(j = 5; 5 < 5; 5++)
The condition (5 < 5) is False. So, compiler will exit from the Second For loop
Next, it will enter into the If statement
if(FreqArr[i] != 0)
FreqArr[0] = Count => 2

Do the same for remaining iterations.

At last, we used For Loop to print array. I suggest you to refer Print Array Elements article to understand the Loop

C Program to Count Frequency of each Element in an Array Example 2

In our previous program, we are counting the frequency of each element in an array. This program allows the user to choose the array element and check the frequency that particular element using IF Else statement

/* C Program to Delete Duplicate Element from an Array */
 
#include <stdio.h>
 
int main()
{
	int arr[10], i, j, k, Size;
	
	printf("\n Please Enter Number of elements in an array  :   ");
	scanf("%d", &Size);
	
	printf("\n Please Enter %d elements of an Array \n", Size);
	for (i = 0; i < Size; i++)
	{
    	scanf("%d", &arr[i]);
   	}     
 
	for (i = 0; i < Size; i++)
	{
		for(j = i + 1; j < Size;)
		{
    		if(arr[i] == arr[j])
    		{
    			for(k = j; k < Size; k++)
    			{
    				arr[k] = arr[k + 1];
				}
				Size--;
			}
			else
			{
				j++;
			}
		}
	}

 	printf("\n Final Array after Deleteing Duplicate Array Elements is:\n");
 	for (i = 0; i < Size; i++)
  	{
 		printf("%d\t", arr[i]);
  	}	     
 	return 0;
}
C Program to Count Frequency of each Element in an Array 2