Find Most Frequent Element in an Array

Find Most Frequent Element in an Array” is one of the favourite technical interview problem based on array data structutre. Here, we are given an array of ‘n’ elements and Our task is to find the most frequent element in an array. If there are multiple elements with same highest frequency, return the one element which appears first.

Example (find most frequent element in an array):

INPUT:
Arr[] = {1, 2, 3, 4, 2, 3, 4, 2}
OUTPUT:
2
EXPLANATION:
2 because it occurs most number of times

There are multiple methods to find the most frequent element in an array. Some of them are:

METHOD 1: Brute-Force Approach to find most frequent element in an array

The brute force solution of the given problem would be to run two nested loops and count the occurrence of each element of an array. While counting occurrences of an elements, store the value of highest frequent element in temporary variable. Finally, returns and print the temporary variable.

The time complexity of this solution is O(n2), where ‘n’ is the size of an array.

C++ Program to find Most Frequent Element in an Array is as follows:

/* C++ Program to find most frequent element in an array */
#include<bits/stdc++.h>
using namespace std;
void highestFrequent(int arr[],int n)
{
    int res = arr[0];
    int count = 1;
    for(int i = 0 ; i< n ; i++)
    {
        int temp_count = 0;
        for(int j = 0 ; j < n ; j++)
        {
            if(arr[i] == arr[j])
            {
                temp_count++;
            }
        }
        if(temp_count> count)
        {
            res = arr[i];
        }
    }    
    cout<<"The most frequent element of the array is "<<res<<"\n";
}
int main()
{
    int arr[8] = {1, 2, 3, 4, 2, 3, 4, 2};
    highestFrequent(arr,8);
}

OUTPUT:
The most frequent element of the array is 2

METHOD 2: Hashing Based Approach to find most frequent element in an array

The hashing based approach is much more time efficient approach than previous approach, but this approach uses extra auxiliary space.

Simply, store the frequency of all the elements of the array in hashmap.

Now, traverse the hashmap and return the element with highest frequency.

The time complexity of this solution is O(nlogn), where ‘n’ is the size of the array and ‘logn’ for inserting and searching in the hash map.

C++ Program to find most frequent Element in an Array is as follows:

/* C++ Program to find most frequent Element in an Array */
#include<bits/stdc++.h>
using namespace std;
void highestFrequent(int arr[],int n)
{
    /* Creating Hash Map*/
    map<int,int>mp;
    /* Inserting elements and count in Hashmap */
    for(int i = 0; i<n;i++)
    mp[arr[i]]++;
    map<int,int>:: iterator itr;
    int res = arr[0];
    int count = 1;
    /* Scanning Hashmap for most frequent element */
    for(itr = mp.begin(); itr!=mp.end() ; ++itr)
    {
        if(itr -> second > count)
        {
            count = itr->second;
            res = itr->first;
        }
    }
    cout<<"The most frequent element of the array is "<<res<<"\n";
}
int main()
{
    int arr[8] = {1, 2, 3, 4, 2, 3, 4, 2};
    highestFrequent(arr,8);
}

OUTPUT:
The most frequent element of the array is 2

Related Posts:

  1. Find Most Frequent Character of the String.
  2. Check Anagram Strings.
  3. Check Whether Given String is Palindromic Anagram or Not.
  4. Check Whether Given String is Panagram or Not.
  5. Find First Non-Repeating Character of the String.
  6. Find Pair in an Array with Given Sum.
  7. Find First Repeating Element of an Array.
  8. Find Majority Element of an Array.
  9. Program to find factorial of a number.
  10. Program to find GCD of two numbers.
  11. Program to find LCM of two numbers.
  12. Program to check whether entered number is odd or even.
  13. Program to check whether entered number is prime number or not.
  14. Program to check whether entered number is palindrome or not.
  15. Program to check whether entered number is Armstrong Number or Not.
  16. Program to convert binary number to octal number.
  17. Program to convert binary number to decimal number.
  18. Program to convert binary number to hexadecimal number.
  19. Program to convert octal number to binary number.
  20. Program to convert octal number to decimal number.

You may also like...