Count Frequency of Each Element in Array in C Language

Program-to-Count-Frequency-of-Each-Element-in-Array-in-C-Language

Program Description:

We have looked at the Linear Search in C program in earlier Posts, In today’s post, We will look at the Program to Count Frequency of Each Element in Array in C Programming language. The Program should accept an input array from the user and display the frequency of each element in an array.

Let’s look at the example input and output to better understand the program.

Example Input and Output:

Input:

Enter desired size for array(1-100): 7

Please enter array elements(7) : 1 0 1 0 0 0 1

Output:

Original Array: 1 0 1 0 0 0 1

Array Element : 1 and It's Frequency: 3

Array Element : 0 and It's Frequency: 4

As we can see from the above output, The element 1 is repeated three( 3) times in the input array, Similarly, The element Zero(0) has the frequency of 4 in the array.

Prerequisites:

To better understand the following program, We should know the basics of the Arrays and Functions.

Algorithm for Count Frequency of Each Element in Array in C:

  1. We are going to use a Counter Array ( countArr), which is going to store the frequency of the element. The counter Array ( countArr) should be the same size as the input array and initialize its elements with -1
  2. Iterate over that input array ( numbers) and at each iteration,
    • Use another for loop to check the frequency of the current element numbers[i].
      • count the frequency of the current element ( count)
    • If the countArr[i] == 0, Then we updated the frequency( count) of the current element( numbers[i]).
    • Check if the countArr[i] is updated or not, Then update countArr[i] with the calculated frequency( count)
  3. Display the results by going through the countArr array.

Let’s look at the step-by-step explanation of the Count Frequency of each element in an array in the C program.

Count Frequency of Each Element in Array in C Program Explanation:

  1. Declare the input array numbers[100] and the counter array counter[100]. Also, declare other supporting variables.
  2. Take the desired array size from the user and update the size variable.
  3. Check if the array size is out of bounds (max size is 100 – change this if you want to use large arrays), If the array is out of bounds, Display an error message and terminate the program using the return statement.
  4. Take the user input for the numbers array. Also, initialize all elements of the countArr with -1.
    • Use a For loop to iterate over the array element and update numbers[i] and countArr[i]
  5. Print the Input Array Elements on the console by calling the display() helper function.
  6. Now, To Count the Frequency of Each Element in an Array in C language, We need to traverse through the array elements one by one and check the frequency using another array.
  7. Create A For Loop (Let’s call it Outer Loop) to iterate over the Input array. ( for(i = 0; i<size; i++) ), At each iteration,
    • Update the count = 1 The count variable holds the current element ( numbers[i]) frequency.
    • Create another For Loop(Inner Loop) to count the frequency of the current element. for(int j = i+1; j<size; j++),
      • Check if the numbers[i] is equal to numbers[j], If it is true, Then increment the count variable.
    • In the Outer Loop, If the countArr[i] is not updated ( if(countArr[i] != 0)), Then update the countArr[i] with current element frequency – countArr[i] = count;
    • Repeat the above steps till i reaches the size-1
  8. Once the above two loops are completed, Then the countArr will contain the frequencies of each element in the array.
  9. Display the results on the console.

Program to Count Frequency of Each Element in Array in C Language:

Here is the program to count frequency of each element in array.

In the above program, We have used display() function to print the array elements on the console.

Also note, That while printing the frequencies of all array elements, We are considering the numbers and countArr elements.

Program Output:

Let’s Compile and Run the program using GCC compiler (Any Compiler)

Test 1: Provide arrays with duplicate elements.

From the above output, We can see the input array is 1000 6000 2000 3000 1000 6000 3000 1000 6000 1000 and the frequency of the each elements are

Let’s try few more examples

Count-Frequency-of-Each-Element-in-Array-in-C-Program-Output

Test 2: Negative tests – with Invalid Size:

As excepted Program is displaying an error message( Invalid Size, Try again) if the size goes beyond the limits.

Related Array Programs:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

Leave a Reply