使用a
Map<Integer, Integer>应该很简单:
int mostFrequent(int... ary) { Map<Integer, Integer> m = new HashMap<Integer, Integer>(); for (int a : ary) { Integer freq = m.get(a); m.put(a, (freq == null) ? 1 : freq + 1); } int max = -1; int mostFrequent = -1; for (Map.Entry<Integer, Integer> e : m.entrySet()) { if (e.getValue() > max) { mostFrequent = e.getKey(); max = e.getValue(); } } return mostFrequent;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)