java 检查数组中是否存在任何重复的索引值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4869127/
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
Check to see if any duplicate index values exist within an array?
提问by Hanna
So basically, I have one array, with ten values...
所以基本上,我有一个数组,有十个值......
int[] input = new int[10];
The user controls the input to each value.
用户控制每个值的输入。
What would be a good technique to check to see if any of the values inside of the array are equal to any of the other values?
检查数组中的任何值是否等于任何其他值的好方法是什么?
Edit:
编辑:
public static void main(String[] args) {
P2 numbers = new P2();
for (int i = 0; i < numbers.input.length; i++) {
numbers.input[i] = numbers.scan.nextInt();
}
numbers.Check();
if (numbers.Check()) { System.out.println("Duplicate"); }
if (numbers.Check() == false) { System.out.println("NOT Duplicate"); }
}
public boolean Check() {
int length = input.length;
for(int i : input) {
for(int j = i + 1; j < length; j++) {
if(input[i] == input[j]) return true;
}
}
return false;
}
The codes works ask long as duplicate numbers are index-neighbors.
只要重复的数字是索引邻居,代码就可以工作。
回答by StriplingWarrior
If you really only have ten values in the array, you're better off with a double-nested loop that breaks when it finds a duplicate.
如果数组中确实只有 10 个值,那么最好使用双嵌套循环,该循环在找到重复项时会中断。
int length = input.length;
for(int i = 0; i < length; i++) {
for(int j = i + 1; j < length; j++) {
if(intput[i] == input[j]) return true;
}
}?
If you're expecting to scale up to a large number, you're better off populating a hashset and breaking when you find a value that's already in the Hashset.
如果您希望扩展到一个很大的数字,那么最好填充一个哈希集并在找到一个已经在 Hashset 中的值时中断。
HashSet<Integer> set = new HashSet<Integer>();
for(int i : input) {
if(set.contains(i)) return true;
set.add(i);
}?
回答by S?ren
You could
你可以
- compare all values in two loops (inefficient)
- sort the array and then only compare adjacent values. This is more efficient, especially for longer arrays.
- 比较两个循环中的所有值(低效)
- 对数组进行排序,然后只比较相邻的值。这更有效,尤其是对于更长的阵列。
The first one is very easy to implement and if your array only has a length of 10, this should suffice.
第一个很容易实现,如果你的数组只有 10 的长度,这应该就足够了。
回答by Stephen Denne
If the stored int
values are small non-negative values, then a BitSet
might be appropriate:
如果存储的int
值是小的非负值,那么 aBitSet
可能是合适的:
BitSet set = new BitSet();
for(int i : input) {
if(set.get(i)) return true;
set.set(i);
}?
If you instead have a wide range of values, and lots of them, but a low likelihood of there being a duplicate (i.e. you expect there to be no duplicates, and simply need to confirm that), you can hash the ints, and use a BitSet(8192) of the low 13 bits of the hash (for example). That only uses roughly 1k. This can be used to easily confirm that there are no duplicates, but if it does find a hash collision, then you need to re-check with a less efficient method.
如果您有广泛的值,并且有很多,但重复的可能性很小(即您希望没有重复,并且只需要确认),您可以散列整数,并使用散列的低 13 位的 BitSet(8192)(例如)。那只使用大约 1k。这可以用来轻松确认没有重复,但如果确实发现了哈希冲突,则需要使用效率较低的方法重新检查。
回答by Ted Hopp
You can also sort the array and scan quickly for duplicates. Compared to building a hashset, it won't use as much memory, but will be slower.
您还可以对数组进行排序并快速扫描重复项。与构建哈希集相比,它不会使用那么多内存,但会更慢。