【问题标题】:How do you remove duplicate values then display unique values?如何删除重复值然后显示唯一值?
【发布时间】:2013-04-10 10:56:29
【问题描述】:
public class nrlSports {
    public static void main(String[] args){
        String[] direction = {"north", "north", "east", "south", "south", "south", "north", "north"};

        for(int i=0; i<direction.length; i++) {
            int count = 0;
            for(int j = 0; j < direction.length; j++)
                if(direction[i].equals(direction[j])) {
                    count++;
                }
            System.out.println(direction[i] + " (" + count + ")");
        }
    }
}

输出是: 北 (4) 北 (4) 东 (1) 南 (3) 南 (3) 南 (3) 北 (4) 北 (4)

如何删除这些重复值,使输出应如下所示: 北 (4) 东 (1) 南 (3)

【问题讨论】:

  • 将数组的所有元素添加到Set
  • 数组会一直排序吗?

标签: java arrays count duplicates output


【解决方案1】:

认为解决方案不是优化的,但可以工作(通过运行确认:))

        String[] direction = {"north", "north", "east", "south", "south", "south", "north", "north"};
    HashMap<String,Integer> myHash = new HashMap<String, Integer>();


    for(int i=0; i<direction.length; i++) {
        int count = 0;
        for(int j = 0; j < direction.length ; j++)
            if(direction[i].equals(direction[j])) {
                count++;
                myHash.put(direction[i], new Integer(count));                    
            }
    }

       Iterator T = (Iterator) myHash.entrySet().iterator();
       while( T.hasNext() ) 
       {
            Map.Entry newEntery = (Map.Entry) T.next();                
            System.out.print(newEntery.getKey() +"("+ newEntery.getValue()+")");
       } 

【讨论】:

    【解决方案2】:

    这会有所帮助:

    public static void main(String[] args){
        String[] direction = {"north", "north", "east", "south", 
                              "south", "south", "north", "north"};
        Map<String, Integer> countMap = new HashMap<String, Integer>();
    
        for(int i=0; i<direction.length; i++) {
            String key = direction[i];
            if(!countMap.containsKey(key)){
                countMap.put(key, 1);
            }
            else 
            {
                countMap.put(key, countMap.get(key)+1);
            }
        }
        System.out.println(countMap);
    }
    

    【讨论】:

      【解决方案3】:

      这个方法怎么样

      String[] direction = { "north", "north", "east", "south", "south",
              "south", "north", "north" };
      
      Map<String, Integer> map = new LinkedHashMap<>();//to preserve order or elements
      for (String key : direction) {
          if (map.containsKey(key))
              map.put(key, map.get(key) + 1);
          else
              map.put(key, 1);
      }
      for (Map.Entry<String, Integer> entry : map.entrySet())
              System.out.println(entry.getKey() + "(" + entry.getValue() + ")"+
                      String.format("%"+entry.getValue()+"s", "").replace(' ', '*'));
      

      输出

      north(4)****
      east(1)*
      south(3)***
      

      【讨论】:

      • 是的,这很完美。你怎么加星*,例如北(4):****东(1):*南(3):***
      • 非常感谢。我不明白的一件事是'\u0000'。这是做什么的?
      • @Shuvo0o 我更改了代码,不再使用\u0000。默认情况下,空字符数组填充有\u0000,这是第一个 unicode 字符,因此整个字符串也只包含它们。现在我使用String.format("%"+entry.getValue()+"s", "")。如果您像String.format("%5s", "") 一样使用它,它将生成包含正好5 个空格" " 的字符串。后来我用*替换了每个空格。
      • @Shuvo0o 由于您是 SO 的新用户,您可能不知道如果某些答案解决了您所描述的问题,您应该 accept it
      【解决方案4】:

      我可能的简单方法是

      Set<String> directionS= new HashSet<String>(Arrays.asList(direction));
      

      然后和directionS一起玩

      【讨论】:

        【解决方案5】:

        使用一个集合。将它们全部添加到一个集合中,您将获得唯一值。

        编辑: 我错过了计数部分。现在添加它

        String[] direction = {"north", "north", "east", "south", 
                                  "south", "south", "north", "north"};
        
            Map<String,int> m = new HashMap<String,int>();
        
         for(String str:direction){
            if(m.contains(str))
               m.put(key,m.get(str)+1);
            else
               m.put(key,1);
        }
        
        
        //key is all values you want to add and count in no. of times values is repeated.
        

        因此,如果您想要所有唯一值,您可以遍历键集并从 Map 中获取相应的计数。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-11-01
          • 1970-01-01
          • 1970-01-01
          • 2014-10-02
          • 1970-01-01
          • 2014-11-05
          • 2011-11-01
          • 1970-01-01
          相关资源
          最近更新 更多