如何删除重复值然后显示唯一值?

时间:2013-04-17 15:27:36

标签: java arrays count duplicates output

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)

5 个答案:

答案 0 :(得分:1)

这可以提供帮助:

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);
}

答案 1 :(得分: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 :(得分:0)

使用一套。将所有这些添加到集合中,您将获得唯一值。

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

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获取相应的计数。

答案 3 :(得分:0)

我可能的简单方法是

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

然后使用directionS

进行游戏

答案 4 :(得分:0)

这种方法怎么样

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)***