Created
December 16, 2015 11:22
-
-
Save yelinaung/1067b35ee2aab87dd71a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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