0

I have this following piece of code to find the maximum frequency number in an array

import java.util.*;
public class Solution {

public static int maxFrequencyNumber(int[] arr){
    Map<Integer,Integer> m = new HashMap<>();
    for(int i:arr)
        m.put(i,m.getOrDefault(i,0)+1);
    int num,max=Integer.MIN_VALUE;
    for(Map.Entry e:m.entrySet()){
        if(e.getValue().intValue()>max){
            num=e.getKey().intValue();
            max=e.getValue().intValue();
        }
    }
    return max;

}

}

Now the issue with this is Map.Entry e.getValue() returns Object type and .intValue doesn't work on Object (only Integer objects I guess) so is there a way to extract the integer value stored in a variable of type Object?

1
  • A raw type like Map.Entry should never be used. If the correct type arguments are given, then the abovementioned problem disapprears as well. Commented Apr 28, 2020 at 10:18

4 Answers 4

2

You would have to use generic types for Entry:

  for(Map.Entry<Integer,Integer> e:m.entrySet()){
    if(e.getValue().intValue()>max){
        num=e.getKey().intValue();
        max=e.getValue().intValue();
    }
Sign up to request clarification or add additional context in comments.

Comments

1

There are two ways to deal with the problem. Also, you can simplify your code by using Math.max.

1. Replace the raw type with the parameterized type (the recommended way):

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static void main(String[] args) {
        System.out.println(maxFrequencyNumber(new int[] { 10, 20, 30, 10, 30, 40, 10, 20, 30, 40, 10 }));
    }

    public static int maxFrequencyNumber(int[] arr) {
        Map<Integer, Integer> m = new HashMap<>();
        for (int i : arr) {
            m.put(i, m.getOrDefault(i, 0) + 1);
        }
        int num, max = Integer.MIN_VALUE;
        for (Entry<Integer, Integer> e : m.entrySet()) {
            max = Math.max(max, e.getValue());
        }
        return max;
    }
}

2. Cast e.getValue() to (Integer):

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        System.out.println(maxFrequencyNumber(new int[] { 10, 20, 30, 10, 30, 40, 10, 20, 30, 40, 10 }));
    }

    public static int maxFrequencyNumber(int[] arr) {
        Map<Integer, Integer> m = new HashMap<>();
        for (int i : arr) {
            m.put(i, m.getOrDefault(i, 0) + 1);
        }
        int num, max = Integer.MIN_VALUE;
        for (Map.Entry e : m.entrySet()) {
            max = Math.max(max, (Integer) e.getValue());
        }
        return max;
    }
}

Output:

4

Additional note: Since you do not need keys in the map, you could simply iterate Map::values as follows:

import java.util.HashMap;
import java.util.Map;

public class Main {
    public static void main(String[] args) {
        System.out.println(maxFrequencyNumber(new int[] { 10, 20, 30, 10, 30, 40, 10, 20, 30, 40, 10 }));
    }

    public static int maxFrequencyNumber(int[] arr) {
        Map<Integer, Integer> m = new HashMap<>();
        for (int i : arr) {
            m.put(i, m.getOrDefault(i, 0) + 1);
        }
        int num, max = Integer.MIN_VALUE;
        for (Integer i : m.values()) {
            max = Math.max(max, i);
        }
        return max;
    }
}

Comments

0

You defined a Map<Integer, Integer> map so you can iterate over its keys erasing your Map.Entry problem like below:

int num, max = Integer.MIN_VALUE;
for (Integer key : m.keySet()) {
    int value = m.get(key);
    if (value > max) {
        num = key;
        max = value;
    }
}

1 Comment

It's more efficient to iterate over the entry set, if you need keys and values.
0

Your codes with casting

  public static int maxFrequencyNumber(int[] arr){
      Map<Integer,Integer> m = new HashMap<>();
      for(int i:arr)
          m.put(i,m.getOrDefault(i,0)+1);
      int num,max=Integer.MIN_VALUE;
      for(Map.Entry e:m.entrySet()){
          if(((Integer)e.getValue()).intValue()>max){
              num=((Integer)e.getKey()).intValue();
              max=((Integer)e.getValue()).intValue();
          }
      }
      return max; 
  }

1 Comment

Generics are the recommended way

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.