public static int getFreq(List<Integer> list) { | |
int mostFreq = 0; | |
//System.out.println("list is " + list); | |
Map<Integer, Integer> map = new HashMap<>(); | |
for (int i : list) { | |
int freq = Collections.frequency(list, i); | |
map.put(i, freq); | |
} | |
Map.Entry<Integer, Integer> max = null; | |
for (Map.Entry<Integer, Integer> entry : map.entrySet()) { | |
if (max == null || entry.getValue() > max.getValue()) { | |
max = entry; | |
} | |
} | |
System.out.println(map.toString()); | |
if (max != null) { | |
mostFreq = max.getKey(); | |
//System.out.println(max.getKey()); | |
//System.out.println(max.getValue()); | |
} | |
return mostFreq; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment