在java中,如何检查一维数组中的元素是否彼此不同?

时间:2022-01-26 11:58:27

For example,I have an 1-D array ,how can I check if all the elements are different?(no one same with other,all of them should be different) Use for-loop?Or something else?

例如,我有一个一维数组,如何检查所有元素是否不同?(没有一个与其他元素相同,所有元素都应该不同)使用for-loop?还是其他什么?

I thought maybe this,but cannot be true:

我想也许这样,但不可能是真的:

int []array={1,2,3,4,4,6}

for(int i=0;i<a.length;i++){
  if(a[i]!=a[i+1])
    return true;
  else 
    return false;

}

}

so is there some good method can use to check different?

那么是否有一些好的方法可以用来检查不同?

4 个解决方案

#1


7  

Add all items from the array into a Set and check the set size with the original array size.

将数组中的所有项添加到Set中,并使用原始数组大小检查集大小。

If they're different sizes, then there's a duplicate element.

如果它们的大小不同,那么就有一个重复的元素。

#2


2  

Create a HashSet (or another kind of Set), and loop through the elements of your array. Add them one at a time. If the element already exists in the Set, then not all are different and you can return false. You can tell this if add returns false; then you don't have to check the rest of the array. If you finish with the entire array and none are the same, then they're all different and you can return true.

创建一个HashSet(或其他类型的Set),并循环遍历数组的元素。一次添加一个。如果元素已经存在于Set中,那么并非所有元素都不同,您可以返回false。如果add返回false,你可以告诉它;那么你不必检查数组的其余部分。如果你完成整个数组并且没有一个是相同的,那么它们都是不同的,你可以返回true。

The problem with your current code is that you only check the first and second elements, and you return something right away based on that comparison, and you don't consider the rest of the elements.

您当前代码的问题在于您只检查第一个和第二个元素,并根据该比较立即返回一些内容,而您不考虑其余元素。

#3


0  

There are several ways to do this. For example, you can use two for loops:

有几种方法可以做到这一点。例如,您可以使用两个for循环:

boolean hasDuplicates(int[] array) {
    for (int i = 0; i < array.length; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if (array[i] == array[j]) {
                return true;
            }
        }
    }
    return false;
}

If that’s too much writing for you, you can just create a Set from the array and compare the size of the set and the array. The set automatically drops duplicate entries.

如果这对你来说太多了,你可以从数组中创建一个Set,并比较集合和数组的大小。该集自动删除重复的条目。

#4


0  

Set<String> values = new HashSet<>(Arrays.asList(array));
assertEquals(array.length, values.size());

#1


7  

Add all items from the array into a Set and check the set size with the original array size.

将数组中的所有项添加到Set中,并使用原始数组大小检查集大小。

If they're different sizes, then there's a duplicate element.

如果它们的大小不同,那么就有一个重复的元素。

#2


2  

Create a HashSet (or another kind of Set), and loop through the elements of your array. Add them one at a time. If the element already exists in the Set, then not all are different and you can return false. You can tell this if add returns false; then you don't have to check the rest of the array. If you finish with the entire array and none are the same, then they're all different and you can return true.

创建一个HashSet(或其他类型的Set),并循环遍历数组的元素。一次添加一个。如果元素已经存在于Set中,那么并非所有元素都不同,您可以返回false。如果add返回false,你可以告诉它;那么你不必检查数组的其余部分。如果你完成整个数组并且没有一个是相同的,那么它们都是不同的,你可以返回true。

The problem with your current code is that you only check the first and second elements, and you return something right away based on that comparison, and you don't consider the rest of the elements.

您当前代码的问题在于您只检查第一个和第二个元素,并根据该比较立即返回一些内容,而您不考虑其余元素。

#3


0  

There are several ways to do this. For example, you can use two for loops:

有几种方法可以做到这一点。例如,您可以使用两个for循环:

boolean hasDuplicates(int[] array) {
    for (int i = 0; i < array.length; i++) {
        for (int j = i + 1; j < array.length; j++) {
            if (array[i] == array[j]) {
                return true;
            }
        }
    }
    return false;
}

If that’s too much writing for you, you can just create a Set from the array and compare the size of the set and the array. The set automatically drops duplicate entries.

如果这对你来说太多了,你可以从数组中创建一个Set,并比较集合和数组的大小。该集自动删除重复的条目。

#4


0  

Set<String> values = new HashSet<>(Arrays.asList(array));
assertEquals(array.length, values.size());