C program to count frequency of each element in an array

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.

Array frequency

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.

  1. Input size and elements in array from user. Store it in some variable say size and arr.
  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 size. The loop structure must look like for(i=0; i<size; i++).
  4. Inside outer loop, initialize count variable with 1 to count total frequency of the currently selected array element.
  5. Run an inner loop to count total duplicates of currently selected array element. Run an inner loop from i + 1 to size. The loop structure should look like for(j = i + 1; j < N; j++).
  6. Inside inner loop, if 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 total duplicate count of current element in frequency array. Which is say freq[i] = count.
  8. 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 😉