计算 Java 字符串数组中的重复元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24052595/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 09:54:15  来源:igfitidea点击:

Counting duplicate elements in a Java String array

javaarraysstringduplicates

提问by user2716340

I have a String array that has been randomly populated by String elements. I would like to iterate through the array and keep count of how many duplicate elements are found. I have found other answers that simply check if there is a duplicate or the position of a duplicate but not how many duplicates exist in the array.

我有一个由 String 元素随机填充的 String 数组。我想遍历数组并计算找到了多少重复元素。我找到了其他答案,它们只是检查是否存在重复项或重复项的位置,但不检查数组中存在多少重复项。

For example:

例如:

String [] arrayToCheck = {"A", "B", "C", "A", "B", "C"};

The number of duplicates found would be 3 (1 of each string is duplicated).

找到的重复项数为 3​​(每个字符串中有 1 个重复)。

I've tried many different things but can't seem to come up with code that gives me the correct answer (the number of duplicates in the array) no matter how many duplicates there are.

我尝试了很多不同的东西,但似乎无法想出代码来给我正确的答案(数组中的重复数),无论有多少重复。

Here's is what I've tried to no avail:

这是我尝试无济于事的内容:

int numDupes = 0;

for (int j = 0; j < arrayToCheck.length; j++) 
     for (int k = j + 1; k < arrayToCheck.length; k++) 
          if (k != j && arrayToCheck[k].equals(arrayToCheck[j])) 
              numDupes++;

AND:

和:

int numDupes = 0;

for (String someVal : arrayToCheck) {
     for (int i = 0; i < arrayToCheck.length; i++) {
          if (arrayToCheck[i].equals(someVal)) {
              numDupes++;
          }
     }
}

AND:

和:

int numDupes = 0;

for(int i = 1; i < arrayToCheck.length; i++) {
    if(arrayToCheck[i].equals(arrayToCheck[i-1])) {
       numDupes++;
    }
}

Thanks!

谢谢!

采纳答案by Aniket Thakur

You can do something like below. Put it in a map with count as value. Then simply count strings with count > 1.

您可以执行以下操作。将其放入以计数为值的地图中。然后简单地用 计算字符串count > 1

    String [] someArray = new String[]{"A", "B", "C", "A", "B", "C"};
    Map<String,Integer> repeatationMap= new HashMap<String,Integer>();
    for(String str : someArray){

        if(repeatationMap.containsKey(str)) {
            repeatationMap.put(str,repeatationMap.get(str) + 1);
        }
        else {
            repeatationMap.put(str, 1);
        }
    }

    int count = 0;
    for(int repatCount : repeatationMap.values()){
        if(repatCount > 1) {
            count++;
        }
    }
    System.out.println("Number of Strings repeated : " + count);

回答by TheLostMind

Convert it to a set.. Then (length_of_Set - length_of_Array) is your count of duplicate elements.

将其转换为集合.. 然后 (length_of_Set - length_of_Array) 是重复元素的计数。

Note : This will not tell you how many times each element is duplicated or which elements are duplicated.

注意:这不会告诉您每个元素被复制多少次或哪些元素被复制。