在现有代码中计算数组(java)中的重复字符串

时间:2018-06-18 03:39:11

标签: java arrays

public static void main(String[] args) {
    String[] a = {"java","c++","c++","java","c#","bootstrap","java"};
    Set<String> nonDuplicateSet = new HashSet<String>();
    Set<String> duplicateSet = new HashSet<String>();
    for(String abc:a){
        if(!nonDuplicateSet.contains(abc)){
            nonDuplicateSet.add(abc);
        }else{
            duplicateSet.add(abc);
        }
    }
    System.out.println(duplicateSet);

如何计算java重复3次/ c ++重复2次

1 个答案:

答案 0 :(得分:0)

这会对你有所帮助

`String[] a = {"java","c++","c++","java","c#","bootstrap","java"};
        Map<String, Integer> map = new HashMap<String, Integer>();
        for(String str :a){
            if(map.containsKey(str)){
                map.put(str, map.get(str)+1);
            }else{
                map.put(str, 1);
            }
        }`

和for entrySet计算。