java 在java数组中查找重复词并返回具有唯一重复词的数组 - 使用方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13924812/
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-10-31 14:28:15  来源:igfitidea点击:

find duplicate words in java array and return array with unique duplicate words - use method

javaarraysduplicates

提问by jumbo

I am new to java, and am confused with the problem. This is what I came up so far. I am still working on it, if I come up with any progress, I'll post it here.

我是 Java 新手,对这个问题感到困惑。这是我到目前为止的想法。我还在努力,如果有任何进展,我会在这里发布。

public class charArray {

public static void main(String[] args) {

    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};
    //output should be ["apple", "ball"]

    checkDuplicate(strArray);
}
public static String[] checkDuplicate(String[] strArray){
    String[] newArray = new String[]{};
    for(int i = 0; i < strArray.length; i++){

        for(int j = 0; j < i; j++){
            if (strArray[i].equals(srtArray[j])){
                newArray = strArray[i];
            }
        }

    }
    return newArray[];

}

}

}



New progress: Ok, my mediocre head has gone this far: (and the solution works) It prints out the unique array of Duplicate elements. But now I need to implement the same code, by calling a method. Any help is appreciated.

新进展:好的,我的平庸头脑已经走了这么远:(并且解决方案有效)它打印出重复元素的唯一数组。但是现在我需要通过调用一个方法来实现相同的代码。任何帮助表示赞赏。

import java.util.*;

public class setChar {

public static void main(String[] args) {
    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};
    Set set = new HashSet();
    Set uniqueSet = new HashSet();
    for(int i = 0; i < strArray.length ; i++ ){
        boolean b = set.add(strArray[i]);
        if(b == false){
            uniqueSet.add(strArray[i]);
        }
    }
    Iterator it = uniqueSet.iterator();
    while (it.hasNext()){
        System.out.println(it.next());
    }
}

}

The output is: ball apple

输出为: 球苹果



Finally implemented with method, with proper return type. Please let me know, if this can be further optimized. Thank all for you suggestions. Here is the working code:

最后用方法实现,具有正确的返回类型。请让我知道,如果这可以进一步优化。谢谢大家的建议。这是工作代码:

public class retUnique {

public static void main(String[] args) {
    String[] strArray = new String[] {"apple", "ball", "cat", "apple", "ball", "apple"};

    System.out.println(printUnique(strArray));
}

public static Set<String> printUnique(String[] strArray){
    Set<String> set = new HashSet<String>();
    Set<String> uniqueSet = new HashSet<String>();
    for(int i = 0; i < strArray.length ; i++ ){
        boolean b = set.add(strArray[i]);
        if(b == false){
            uniqueSet.add(strArray[i]);
        }
    }
    return(uniqueSet);
}

}

回答by Karthik T

One easy option is to use a Setlike containerinstead of an array. It will automatically ensure that only unique values are present.

一个简单的选择是使用Set类似的容器而不是数组。它将自动确保仅存在唯一值。

You can look at examples of TreeSetand HashSet.

您可以查看TreeSetHashSet 的示例。

回答by aug

Something like this should work I believe. Been a while since I coded in Java so if anyone wants to make edits, please do.

我相信这样的事情应该有效。自从我用 Java 编码以来已经有一段时间了,所以如果有人想进行编辑,请做。

public String[] returnDups(String[] strArray) {
    Set<String> set = new HashSet<String>(strArray);
    return set.toArray(new String[0]);
}

But what everyone has been suggesting is the correct idea. Sets make it really easy to remove duplicates so you should utilize them. Let's not reinvent the wheel.

但每个人都提出的建议是正确的。集合使删除重复项变得非常容易,因此您应该使用它们。让我们不要重新发明轮子。

回答by AlexWien

To detect dupplicates, if that is your question, add all elememts of the array to a Set, if set.add() returns false, then it is a dupplicate, so you can add this elemen to your result list of dupplictes.
Or if your question is meaning to return the values without duplicates, then you can retirnthe set, or convert the set to list before returning.

要检测重复项,如果这是您的问题,请将数组的所有元素添加到集合中,如果 set.add() 返回 false,则它是重复项,因此您可以将此元素添加到重复项的结果列表中。
或者,如果您的问题是要返回没有重复的值,那么您可以在返回之前重新设置集合,或将集合转换为列表。