# C program to count frequency of each element in an array

Write a C program to read elements in an array and find frequency of each element in an array. C Program to count the occurrence of each element in an array. Logic to count frequency of each element in an array.

Example

Input

`Input array elements: 5, 10, 2, 5, 50, 5, 10, 1, 2, 2`

Output

```Frequency of 5 = 3
Frequency of 10 = 2
Frequency of 2 = 3
Frequency of 50 = 1
Frequency of 1 = 1```

## Required knowledge

Basic C programming, If else, For loop, Array

## Logic to count frequency of each element of array

Finding frequency of each array element is completely based on finding duplicate elements in an array. In fact you can take this program as modified version of find duplicate program. Here you need to find total duplicate count per element instead of whole.
Below is the step by step descriptive logic to count frequency of each element of array.

1. Read input in all array elements from user. Store it in some variable say arr contains all array elements.
2. Declare another array with same size as of input array size to store frequency of each array elements. Say freq will store frequencies of all array elements.
3. To count frequency of each element we require two loops. One outer loop to select an array element. Second inner loop to find first duplicate element of the currently selected array element by outer loop. Run an outer loop from 0 to N. The loop structure must look like for(i=0; i<N; i++).
4. Inside outer loop, initialize a count variable to count total frequency of the currently selected array element. Say count = 1.
5. Run an inner loop to count total duplicates of currently selected array element. Run an inner loop from i+1 to N. The loop structure should look like for(j=i+1; j<N; j++).
6. Inside inner loop, if a duplicate element is found increment the frequency count of current array element. Which is if(arr[i] == arr[j]) then count++.
7. After all duplicates has been counted. Store the total duplicate count of current element in the frequency array. Which is say freq[i] = count.
8. Finally print the freq array to get frequencies of each array element.

Read more - Program to find unique elements in array

## Program to count frequency of each element of array

``````/**
* C program to count frequency of each element of array
*/

#include <stdio.h>

int main()
{
int arr[100], freq[100];
int size, i, j, count;

/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);

/* Input elements in array */
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);

/* Initially initialize frequencies to -1 */
freq[i] = -1;
}

for(i=0; i<size; i++)
{
count = 1;
for(j=i+1; j<size; j++)
{
/* If duplicate element is found */
if(arr[i]==arr[j])
{
count++;

/* Make sure not to count frequency of same element again */
freq[j] = 0;
}
}

/* If frequency of current element is not counted */
if(freq[i] != 0)
{
freq[i] = count;
}
}

/*
* Print frequency of each element
*/
printf("\nFrequency of all elements of array : \n");
for(i=0; i<size; i++)
{
if(freq[i] != 0)
{
printf("%d occurs %d times\n", arr[i], freq[i]);
}
}

return 0;
}``````

Output

```Enter size of array: 10
Enter elements in array: 5 10 2 5 50 5 10 1 2 2

Frequency of all elements of array :
5 occurs 3 times
10 occurs 2 times
2 occurs 3 times
50 occurs 1 times
1 occurs 1 times```

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside