# Most Frequent Element of an Array

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

**Example:**

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

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

**METHOD 1: Brute-Force**

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(n^2), where ‘n’ is the size of the array.

**C++ Program to find Most Frequent Element of the Array is as follows:**

#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); }

The most frequent element of the array is 2OUTPUT:

**METHOD 2: HASHING BASED**

The hashing based approach is much more time efficient approach, but uses 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 of the Array is as follows:**

#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); }

The most frequent element of the array is 2OUTPUT:

**Related Posts:**

**Program to find pair in an array with given sum.****Program to find first repeating element of an array.****Program to merge two sorted arrays.****Program to find missing number in an array.****Program to sort if array is sorted.****Program to print Alternate Elements of an Array.****Program to swap kth element from beginning to kth element from end in an Array.****Program to print all possible subarrays of the given array.****Program to print kth smallest and kth largest element of an Array.****Program to find equilibrium index of an Array.****Program to find majority element of an Array.****Program to find mean of the Array.****Program to sort an Array of 0s and 1s.****Program to Reverse an Array.****Program to count number of odd numbers and even numbers in an array.****Program to find absolute difference between sum of odd index elements and even index elements.****Program to find smallest and largest element of the array.****Program to count occurrences of a particular element in an array.****Program to search an element in an Array (Linear Search).****Program to Rotate Array Elements by ‘D’ positions.**