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;
}
}
Map.Entryshould never be used. If the correct type arguments are given, then the abovementioned problem disapprears as well.