Java,如果数组包含重复值则返回真
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17795449/
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
Java, Return true if array contains duplicate values
提问by andrsnn
I am trying to have a method (duplicates) return true if a given array called x (entered by user in another method), contains duplicate values. Otherwise it would return false. Rather then checking the entire array, which is initialized to 100, it will check only the amount of values entered, which is kept track of with a global counter: numElementsInX.
如果给定的名为 x 的数组(由用户在另一个方法中输入)包含重复值,我试图让一个方法(重复)返回 true。否则它会返回false。它不会检查初始化为 100 的整个数组,而是只检查输入的值的数量,该数量由全局计数器 numElementsInX 跟踪。
What is the best way to accomplish this?
实现这一目标的最佳方法是什么?
public static boolean duplicates (int [] x)
I am prompting for user data like so:
我正在提示用户数据,如下所示:
public static void readData (int [] x, int i){
Scanner input = new Scanner(System.in);
System.out.println("Please enter integers, enter -999 to stop");
while (i <= 99) {
int temp = input.nextInt();
if(temp == -999){
break;
}
else {
x[i++]=temp;
}
// else
}//end while
printArray(x,i);
}//end readData
public static void printArray(int [] x, int numElementsInX){
int n = numElementsInX;
for (int i = 0; i < n; i++){
System.out.print(x[i] + " ");
}//end for
System.out.println();
}//end printArray
I am sure there is a better way to do this, but this is how I have been taught so far.
我相信有更好的方法可以做到这一点,但到目前为止,这就是我的教学方式。
回答by Andy Thomas
Here is a solution that:
这是一个解决方案:
- Compiles and executes without throwing.
- Uses
numElementsInX
as you requested. - Returns as soon as it finds a duplicate.
- 编译并执行而不抛出。
- 用途
numElementsInX
为您的要求。 - 一旦发现重复就返回。
This approach tests whether each member of the array has been seen before. If it has, the method can return immediately. If it hasn't, then the member is added to the set seen before.
这种方法测试数组的每个成员之前是否都见过。如果有,该方法可以立即返回。如果没有,则将该成员添加到之前看到的集合中。
public static boolean duplicates (int [] x, int numElementsInX ) {
Set<Integer> set = new HashSet<Integer>();
for ( int i = 0; i < numElementsInX; ++i ) {
if ( set.contains( x[i])) {
return true;
}
else {
set.add(x[i]);
}
}
return false;
}
Here's a sample program containing the above code.
回答by RNJ
this should do it.
这应该这样做。
public boolean containsDuplicates(Integer[] x) {
return new HashSet<Integer>(Arrays.asList(x)).size() != x.length
}
You dont need numElementsInX as this is the same as x.length
您不需要 numElementsInX 因为这与 x.length 相同
editafter comment from Louis. Arrays.asList does not work with int arrays.
在路易斯发表评论后进行编辑。Arrays.asList 不适用于 int 数组。
To convert int[] to Integer try this question How to convert int[] to Integer[] in Java?
要将 int[] 转换为 Integer 试试这个问题How to convert int[] to Integer[] in Java?
or do soemthing like this (not tested but from memory)
或者做这样的事情(未经测试,但来自记忆)
Integer[] newArray = new Integer[a.length];
System.arraycopy(a, 0, newArray, 0, a.length);
回答by Anindya Guha
This certainly isn't the most efficient way, but since you don't know about Sets
yet, you can use two loops:
这当然不是最有效的方法,但由于您还不知道Sets
,您可以使用两个循环:
public static boolean duplicates (int [] x){
for (int i=0; i<numElementsInX; i++){
for (int j=i+1; j<numElementsInX; j++){
if (x[j]==x[i]) return true;
}
}
return false;
}
回答by Tushar Bhandari
"set.add()" returns true if the element is not already present in the set and false otherwise. We could make use of that and get rid of "set.contains()" as in the above solution.
如果元素不存在于集合中,则“set.add()”返回真,否则返回假。我们可以利用它并摆脱上面解决方案中的“set.contains()”。
public static boolean duplicates (int[] x, int numElementsInX) {
Set<Integer> myset = new HashSet<>();
for (int i = 0; i < numElementsInX; i++) {
if (!myset.add(x[i])) {
return true;
}
}
return false;
}