Write a C program to input elements in array and find frequency of each element in array. How to count occurrence of each element in array in C programming using loop. Logic to count frequency of each element in array in C program.
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 Input Output, If else, For loop, Array
Logic to count frequency of each element of array
Finding frequency of each array element is based on logic to find duplicate elements in array.
Step by step descriptive logic to count frequency of each element of array.
- Input size and elements in array from user. Store it in some variable say
size
andarr
. - 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. - 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
size
. The loop structure must look likefor(i=0; i<size; i++)
. - Inside outer loop, initialize
count
variable with 1 to count total frequency of the currently selected array element. - Run an inner loop to count total duplicates of currently selected array element. Run an inner loop from
i + 1
tosize
. The loop structure should look likefor(j = i + 1; j < N; j++)
. - Inside inner loop, if duplicate element is found increment the frequency count of current array element. Which is
if(arr[i] == arr[j])
thencount++
. - After all duplicates has been counted. Store total duplicate count of current element in frequency array. Which is say
freq[i] = count
. - Finally print
freq
array to get frequencies of each array element.
Learn more about 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 😉
Recommended posts
- Array and Matrix programming exercises index.
- C program to find maximum and minimum elements in an array.
- C program to search an element in an array.
- C program to merge elements of two arrays.
- C program to sort elements of array in ascending order.
- C program to find reverse of any number.
- C program to left rotate an array.
- C program to right rotate an array.