C Program to Find the Most Frequent Element in an Array

In this article, we will write a C program to find the most frequent element in an array. A most frequent element is nothing but an element which is repeated for the maximum number of times in a given array.

Sample Example:

Input: 
Enter the Size of the Array: 6
Enter the Elements of the Array: 1  2  4  4  2  4

Output:
The Most Frequent Element is: 4
The Frequency of Most Frequent Element is: 3

Explanation: The most repeated element in the array is 4. As it is repeated 3 times, its frequency is 3.

To find the most frequent element in an array, we can use two nested for loops. The outer for loop will pick every element of the array one by one and compare it with every element of the inner for loop.

The inner for loop will actually run for the elements that are after the current element of the outer for loop in the array. For example, if the index of the current element of the outer for loop is i, then the inner for loop will start from (i + 1)th index.

In each iteration of the inner for loop, we calculate the frequency of the current element picked by the outer for loop. This is done by comparing every element of the inner loop with the current element picked by the outer loop.

If the two elements become equal, we add 1 to the frequency of the current element. When the inner loop finishes running, we check if the frequency of the current element is more than the maximum frequency we have so far.

If yes, we consider the current element as the most frequent element and update the value of the maximum frequency with the frequency of the current element. This continues until the end of the array is reached and finally, we get the value of the most frequent element.

Let’s put all this together and write a C program:

// C Program to find the most frequent element in an array
#include<stdio.h>

int main()
{
    int arr[100], size;
    int max_freq = 0, max_freq_elem;
    
    printf("Enter the Size of the Array: ");
    scanf("%d", &size);
    
    printf("Enter the Elements of the Array: ");
    for(int i = 0 ; i < size; i++){
        scanf("%d", &arr[i]);
    }
    
    for(int i = 0; i < size; i++){
        int freq = 1;
        for(int j = i + 1; j < size; j++){
            if(arr[i] == arr[j]){
                freq = freq + 1;
            }
        }
        
        if(freq > max_freq){
            max_freq = freq;
            max_freq_elem = arr[i];
        }
    }
    
    printf("The Most Frequent Element is: %d", max_freq_elem);
    printf("\nThe Frequency of Most Frequent Element is: %d", max_freq);
    
    
    return 0;
}

Output 1:

Enter the Size of the Array: 6
Enter the Elements of the Array: 1 2 4 4 2 4
The Most Frequent Element is: 4
The Frequency of Most Frequent Element is: 3

Output 2:

Enter the Size of the Array: 5
Enter the Elements of the Array: 10 20 20 20 20
The Most Frequent Element is: 20
The Frequency of Most Frequent Element is: 4

In the above program, we have used two variables max_freq and max_freq_elem. In each iteration of the outer loop, we check if the value of the freq is more than the value of the max_freq.

If yes, we update the value of max_freq and max_freq_elem. If no, we do nothing.

I hope you will find this article helpful. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

Leave a Comment