Skip to content

Instantly share code, notes, and snippets.

@yelinaung
Created December 16, 2015 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yelinaung/1067b35ee2aab87dd71a to your computer and use it in GitHub Desktop.
Save yelinaung/1067b35ee2aab87dd71a to your computer and use it in GitHub Desktop.
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