比如 int a[]={1,1,1,2,3,2,4,3,4}; 数据是不固定的我的思路是先去掉重复的,然后再计算,感觉太麻烦了有没有好的方法求大神赐教

解决方案 »

  1.   

    定义一个HashMap,然后遍历数组,把数组中的每个数作为key值,出现一次value值就加1  HashMap<String, Integer> map = new HashMap<String, Integer>();
      int[] a={1,1,1,2,3,2,4,3,4}; 
      for(int i : a){
       if(map.get(i+"")==null){
        map.put(i+"", 1);
       }else{
        map.put(i+"", map.get(i+"")+1);
       }
      }
      System.out.println(map);
      

  2.   

    ++一般用map比较方便,不过既然是int类型,改用Map<Integer, Integer>更好一些,即
    Map<Integer, Integer> map = new HashMap<Integer, Integer>();
    ...