The Wayback Machine - https://web.archive.org/web/20250312210003/https://www.geeksforgeeks.org/frequent-element-array/
Open In App

Most frequent element in an array

Last Updated : 27 Feb, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

Given an array, find the most frequent element in it. If there are multiple elements that appear a maximum number of times, print any one of them.

Examples: 

Input : arr[] = {1, 3, 2, 1, 4, 1}
Output : 1
Explanation: 1 appears three times in array which is maximum frequency.

Input : arr[] = {10, 20, 10, 20, 30, 20, 20}
Output : 20 appears four times in array which is maximum frequency

[Naive Approach] – Nested Loops – O(n^2) Time and O(1) Space

The naive approach involves using two nested loops: the outer loop picks each element, and the inner loop counts the frequency of the picked element. This method is straightforward but inefficient.

C++
// CPP program to find the most frequent element in an array.
#include <bits/stdc++.h>
using namespace std;

int mostFrequent(vector<int> &arr)
{
    int n = arr.size(), maxcount = 0;
    int res;
    for (int i = 0; i < n; i++) {
        int count = 0;
        for (int j = 0; j < n; j++) {
            if (arr[i] == arr[j])
                count++;
        }

        if (count > maxcount) {
            maxcount = count;
            res = arr[i];
        }
    }

    return res;
}

// Driver program
int main()
{
    vector<int> arr = { 40, 50, 30, 40, 50, 30, 30 };
    cout << mostFrequent(arr);
    return 0;
}
Java
class GfG {
    static int mostFrequent(int[] arr) {
        int n = arr.length, maxCount = 0, res = arr[0];

        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                if (arr[i] == arr[j]) count++;
            }

            if (count > maxCount) {
                maxCount = count;
                res = arr[i];
            }
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr = {40, 50, 30, 40, 50, 30, 30};
        System.out.println(mostFrequent(arr));
    }
}
Python
def most_frequent(arr):
    n, max_count, res = len(arr), 0, arr[0]

    for i in range(n):
        count = sum(1 for j in range(n) if arr[i] == arr[j])

        if count > max_count:
            max_count = count
            res = arr[i]

    return res

arr = [40, 50, 30, 40, 50, 30, 30]
print(most_frequent(arr))
JavaScript
function mostFrequent(arr) {
    let n = arr.length, maxCount = 0, res = arr[0];

    for (let i = 0; i < n; i++) {
        let count = 0;
        for (let j = 0; j < n; j++) {
            if (arr[i] === arr[j]) count++;
        }

        if (count > maxCount) {
            maxCount = count;
            res = arr[i];
        }
    }
    return res;
}

let arr = [40, 50, 30, 40, 50, 30, 30];
console.log(mostFrequent(arr));

Output
30

[Better Approach] – Using Sorting – O(n Log n) Time and O(1) Space

This method sorts the array first and then finds the maximum frequency by linearly traversing the sorted array. Sorting brings similar elements next to each other, making frequency counting easier.

C++
// CPP program to find the most frequent element
#include <bits/stdc++.h>
using namespace std;

int mostFrequent(vector<int>& arr)
{
    // Sort the array
    sort(arr.begin(), arr.end());

    // Find the max frequency using linear traversal
    int max_count = 1, res = arr[0], curr_count = 1;
    for (int i = 1; i < arr.size(); i++) {
        if (arr[i] == arr[i - 1])
            curr_count++;
        else
            curr_count = 1;
        
        if (curr_count > max_count) {
            max_count = curr_count;
            res = arr[i - 1];
        }
    }

    return res;
}

// Driver program
int main()
{
    vector<int> arr = { 40,50,30,40,50,30,30};
    cout << mostFrequent(arr);
    return 0;
}
Java
// Java program to find the most frequent element
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class MostFrequent {
    public static int mostFrequent(int[] arr) {
        
        // Sort the array
        Arrays.sort(arr);

        // Find the max frequency using linear traversal
        int max_count = 1, res = arr[0], curr_count = 1;
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] == arr[i - 1])
                curr_count++;
            else
                curr_count = 1;

            if (curr_count > max_count) {
                max_count = curr_count;
                res = arr[i - 1];
            }
        }

        return res;
    }

    // Driver program
    public static void main(String[] args) {
        int[] arr = { 40, 50, 30, 40, 50, 30, 30 };
        System.out.println(mostFrequent(arr));
    }
}
Python
def most_frequent(arr):
    
    # Sort the array
    arr.sort()

    # Find the max frequency using linear traversal
    max_count = 1
    res = arr[0]
    curr_count = 1
    for i in range(1, len(arr)):
        if arr[i] == arr[i - 1]:
            curr_count += 1
        else:
            curr_count = 1
        
        if curr_count > max_count:
            max_count = curr_count
            res = arr[i - 1]

    return res

# Driver program
arr = [40, 50, 30, 40, 50, 30, 30]
print(most_frequent(arr))
C#
// C# program to find the most frequent element
using System;
using System.Linq;

public class MostFrequent {
    public static int MostFrequent(int[] arr) {
        // Sort the array
        Array.Sort(arr);

        // Find the max frequency using linear traversal
        int max_count = 1, res = arr[0], curr_count = 1;
        for (int i = 1; i < arr.Length; i++) {
            if (arr[i] == arr[i - 1])
                curr_count++;
            else
                curr_count = 1;

            if (curr_count > max_count) {
                max_count = curr_count;
                res = arr[i - 1];
            }
        }

        return res;
    }

    // Driver program
    public static void Main() {
        int[] arr = { 40, 50, 30, 40, 50, 30, 30 };
        Console.WriteLine(MostFrequent(arr));
    }
}
JavaScript
// JavaScript program to find the most frequent element
function mostFrequent(arr) {
    
    // Sort the array
    arr.sort((a, b) => a - b);

    // Find the max frequency using linear traversal
    let max_count = 1, res = arr[0], curr_count = 1;
    for (let i = 1; i < arr.length; i++) {
        if (arr[i] === arr[i - 1])
            curr_count++;
        else
            curr_count = 1;

        if (curr_count > max_count) {
            max_count = curr_count;
            res = arr[i - 1];
        }
    }

    return res;
}

// Driver program
const arr = [40, 50, 30, 40, 50, 30, 30];
console.log(mostFrequent(arr));

Output
30

[Expected Approach] – Using Hashing – O(n) Time and O(n) Space

Using a hash table, this approach stores each element’s frequency and then finds the element with the maximum frequency.

C++
// CPP program to find the most frequent element
// in an array.
#include <bits/stdc++.h>
using namespace std;

int mostFrequent(int arr[], int n)
{
    // Insert all elements in hash.
    unordered_map<int, int> freq;
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;

    // find the max frequency
    int max_count = 0, res = -1;
    for (auto i : freq) {
        if (max_count < i.second) {
            res = i.first;
            max_count = i.second;
        }
    }

    return res;
}

int main()
{
    int arr[] = {40,50,30,40,50,30,30 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << mostFrequent(arr, n);
    return 0;
}
Java
// Java program to find the most frequent element
// in an array
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

class GFG {

    static int mostFrequent(int arr[], int n)
    {

        // Insert all elements in hash
        Map<Integer, Integer> hp
            = new HashMap<Integer, Integer>();

        for (int i = 0; i < n; i++) {
            int key = arr[i];
            if (hp.containsKey(key)) {
                int freq = hp.get(key);
                freq++;
                hp.put(key, freq);
            }
            else {
                hp.put(key, 1);
            }
        }

        // find max frequency.
        int max_count = 0, res = -1;

        for (Entry<Integer, Integer> val : hp.entrySet()) {
            if (max_count < val.getValue()) {
                res = val.getKey();
                max_count = val.getValue();
            }
        }

        return res;
    }

    public static void main(String[] args)
    {

        int arr[] = { 40, 50, 30, 40, 50, 30, 30 };
        int n = arr.length;

        System.out.println(mostFrequent(arr, n));
    }
}
Python
# Python3 program to find the most 
# frequent element in an array.
import math as mt

def mostFrequent(arr, n):

    # Insert all elements in Hash.
    Hash = dict()
    for i in range(n):
        if arr[i] in Hash.keys():
            Hash[arr[i]] += 1
        else:
            Hash[arr[i]] = 1

    # find the max frequency
    max_count = 0
    res = -1
    for i in Hash: 
        if (max_count < Hash[i]): 
            res = i
            max_count = Hash[i]
        
    return res
  
arr = [ 40,50,30,40,50,30,30] 
n = len(arr)
print(mostFrequent(arr, n))
C#
// C# program to find the most 
// frequent element in an array
using System;
using System.Collections.Generic;

class GFG
{
    static int mostFrequent(int []arr, 
                            int n)
    {
        // Insert all elements in hash
        Dictionary<int, int> hp = 
                    new Dictionary<int, int>();
        
        for (int i = 0; i < n; i++)
        {
            int key = arr[i];
            if(hp.ContainsKey(key))
            {
                int freq = hp[key];
                freq++;
                hp[key] = freq;
            }
            else
                hp.Add(key, 1);
        }
        
        // find max frequency.
        int min_count = 0, res = -1;
        
        foreach (KeyValuePair<int, 
                    int> pair in hp)
        {
            if (min_count < pair.Value)
            {
                res = pair.Key;
                min_count = pair.Value;
            }
        } 
        return res;
    }
    
    static void Main ()
    {
        int []arr = new int[]{40,50,30,40,50,30,30};
        int n = arr.Length;
        
        Console.Write(mostFrequent(arr, n));
    }
}
JavaScript
// Javascript program to find
// the most frequent element
// in an array.

function mostFrequent(arr, n)
{
    // Insert all elements in hash.
    var hash = new Map();
    for (var i = 0; i < n; i++) {
        if (hash.has(arr[i]))
            hash.set(arr[i], hash.get(arr[i]) + 1)
            else hash.set(arr[i], 1)
    }

    // find the max frequency
    var max_count = 0, res = -1;
    hash.forEach((value, key) => {
        if (max_count < value) {
            res = key;
            max_count = value;
        }
    });

    return res;
}

var arr = [ 40, 50, 30, 40, 50, 30, 30 ];
var n = arr.length;
console.log(mostFrequent(arr, n));

Output
30




Next Article
Article Tags :
Practice Tags :

Similar Reads

three90RightbarBannerImg