找出数组中重复最多的数

  1. public static int getRepeatMost(int a[]){
  2. Map<Integer,Integer> map = new HashMap<Integer,Integer>();
  3. //记录每个元素出现的次数
  4. for(int i = 0;i < a.length;i++){
  5. if(map.containsKey(a[i])){
  6. map.put(a[i], map.get(a[i])+1);
  7. }else{
  8. map.put(a[i], 1);
  9. }
  10. }
  11. //找出出现次数最多的元素
  12. int most = 0;
  13. int result = 0;
  14. Set<Integer> set = map.keySet();
  15. Iterator<Integer> it = set.iterator();
  16. while(it.hasNext()){
  17. Integer key = it.next();
  18. Integer value = map.get(key);
  19. if(value > most){
  20. most = value;
  21. result = key;
  22. }
  23. }
  24. return result;
  25. }