'Maximum Frequency Number in an array using Hashmaps

Problem:

You are given an array of integers that contain numbers in random order. Write a program to find and return the number which occurs the maximum times in the given input.

If two or more elements contend for the maximum frequency, return the element which occurs in the array first.

Input Format:

Line 1: An Integer N i.e. size of array
Line 2: N integers which are elements of the array, separated by spaces

Output Format:

Most frequent element

Constraints:

0 <= N <= 10^8

Sample Input 1:

13
2 12 2 11 12 2 1 2 2 11 12 2 6 

Sample Output 1:

2

The output is incorrect, please tell what is wrong. Here is the code:

#include <unordered_map>
using namespace std;


int highestFrequency(int *input, int n){
    unordered_map<int, int> map;
    int maxFreq = 0;
    for(int i = 0; i < n; i++){
        if(map.count(input[i]) > 0){
            map[input[i]]++;
            if(map[input[i]] > maxFreq){
                maxFreq = map[input[i]];
            }
        }
        map[input[i]] = 1;
    }
    for(int i = 0; i < n; i++){
        if(map[input[i]] == maxFreq){
            cout << input[i];
        }
    }

    /* Don't write main().
     * the input array is already passed as function argument.
     * Taking input and printing output is handled automatically.
     */ 
}


Solution 1:[1]

I think is an efficient way to count the frequency of the elements. unordered_map mp;

// Traverse through array elements and 
// count frequencies 
for (int i = 0; i < n; i++) 
    mp[arr[i]]++; 

// Traverse through map and print frequencies 
for (auto x : mp) 
    cout << x.first << " " << x.second << endl; 
// found the most frequent item.
 int max_count = 0, res = -1; 
for (auto i : mp) { 
    if (max_count < i.second) { 
        res = i.first; 
        max_count = i.second; 
    } 
} 

res is the answer

Solution 2:[2]

// java solution

public static int maxFrequencyNumber(int[] arr){ 
    if(arr.length == 0)
        return -1;
    int maxFreq = 0;
    int number = -1;
    HashMap<Integer,Integer> map = new HashMap<>();
    
    for(int i=0;i<arr.length;i++)
    {
        if(map.containsKey(arr[i]))
        {
            map.put(arr[i],map.get(arr[i])+1);
        }
        else {
            map.put(arr[i], 1);
        }
    }
    // using set data structure
    Set<Integer> keySet = map.keySet();
    for(Integer i:keySet)
    {
        if(map.get(i) > maxFreq)
        {
            number = i;
            maxFreq = map.get(i);
        }
    }
    return number;
}

}

Solution 3:[3]

I think this is one of the coding ninja questions from a hashmap. This is my code and its correct you can do a dry run you will easily understand

int main(){
    int n;
    cin>>n;
    int arr[n];
    int maxcount=0;
    int maxf=INT32_MIN;
    unordered_map<int,int> freq;

    for(int i=0;i<n;i++){
            cin>>arr[i];
            freq[arr[i]]++;
            if(freq[arr[i]] > maxcount){
                maxcount=freq[arr[i]];
                maxf = arr[i];
            }
        } 
    cout<<maxf<<endl;
    
}
  1. You can just use freq[arr[i]]++ It will automatically update the key and when the new key is found it will create and add value to it.
  2. Making a count variable and updating it whenever we found greater occurrence and storing that key value in maxf and after that returning it;

Solution 4:[4]

import java.util.HashMap;

public class MaimumFrequencyNumber {

public static int maxFrequencyNumber(int[] arr){ 
    HashMap<Integer, Integer> hm = new HashMap<>();
    for (int i : arr) {
        hm.put(i, hm.getOrDefault(i, 0) + 1);
    }
    int max = 0, ans = Integer.MIN_VALUE;
    for (int i : arr) {
        if (hm.get(i) > max) {
            max = hm.get(i);
            ans = i;
        }

    }
    return ans;
}
public static void main (String[] args) {
     
    int arr[] = {1, 5, 2, 1, 3, 2, 1}; 
     
    System.out.println(maxFrequencyNumber(arr));
}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 melk
Solution 2 Amit Rana
Solution 3 Rohit Kumar
Solution 4 Sanchit Uppal