Java 检查行和列中的重复二维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21922354/
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 duplicate in rows and columns 2D Array
提问by user3335070
how would I go about checking a duplicate for columns and rows and return true or false depending if there's duplicates. For example
我将如何检查列和行的重复项并根据是否存在重复项返回 true 或 false。例如
1 2 3
1 2 3
3 1 2
3 1 2
2 3 1
2 3 1
Would return true because no duplicates, but..
会返回 true 因为没有重复,但是..
1 2 2
1 2 2
3 2 3
3 2 3
2 1 1
2 1 1
would return false because there is a duplicate in column 2 {2, 2, 1}.
将返回 false,因为第 2 列 {2, 2, 1} 中存在重复项。
How would I go about checking for if there are in duplicates in the rows and then checking if there are duplicates in the columns?
我将如何检查行中是否存在重复项,然后检查列中是否存在重复项?
So far I have only the following:
到目前为止,我只有以下几点:
public static boolean hasDuplicates(int [][] inArray)
{
for (int row = 0; row < inArray.length; row++)
{
int rowCheck = inArray[row][inArray.length];
for (int col = 0; col < inArray[row].length; col++)
{
}
}
return false;
}
So I need to take in an array and check if there are any duplicates among the columns or rows. Any pointers? Thank you!
所以我需要接收一个数组并检查列或行之间是否有任何重复项。任何指针?谢谢!
NOTE:This cannot be done with outside methods
注意:这不能用外部方法完成
采纳答案by mdewitt
Go through each value in the row. For every value, check and see if any of the values after that value are the same. If the value is the same, return true (you've found a duplicate). If none of the values are the same, increment your index and do the same for the next row. Every row will take at most n(n+1)/2 comparisions which isn't wonderful. So if n is the number of columns and m in the number of rows, this will run, worst case m(n(n+1)/2) times.
遍历行中的每个值。对于每个值,检查并查看该值之后的任何值是否相同。如果值相同,则返回 true(您发现了重复项)。如果所有值都不相同,请增加索引并对下一行执行相同操作。每行最多需要 n(n+1)/2 次比较,这并不好。因此,如果 n 是列数,m 是行数,这将运行,最坏的情况是 m(n(n+1)/2) 次。
Here is an example of how it would work for the rows:
以下是它如何处理行的示例:
/**
* Return flag indicating if there are duplicates in the rows of the 2D array
*
* @return true if a row has duplicates, else false
*/
public boolean hasDuplicatesInRows(int[][] inArray)
{
for (int row = 0; row < inArray.length; row++)
{
for (int col = 0; col < inArray[row].length; col++)
{
int num = inArray[row][col];
for (int otherCol = col + 1; otherCol < inArray.length; otherCol++)
{
if (num == inArray[row][otherCol])
{
return true;
}
}
}
}
return false;
}
That would be pretty easy to extend to do columns as well. I will leave that for you to do though.
这也很容易扩展到做列。不过,我会把它留给你去做。
If you used an efficient sorting method and sorted all the rows, you could just go down the line and see if any value was equal to the value after it. If it is, return true, else return false. This would be more efficient if you had a large data set.
如果您使用了一种高效的排序方法并对所有行进行了排序,您就可以顺行查看是否有任何值等于其后的值。如果是,返回真,否则返回假。如果你有一个大数据集,这会更有效。
回答by h_k
for rows, try following this sort of procedure:
对于行,请尝试执行以下此类程序:
boolean dup = false;
for (int k = 0; k < inArray[0].length){ //loop through columns
for (i = 0; i < inArray.length-1; i++) {
for (int j = i; j < inArray.length; j++){
if (inArray[k][i] == inArray[k][j]){
dup = true;
break;
}
}
}
}
so, you're starting at the first element, then scanning from element 2 to n (ie number of columns). If a match is found, then you're setting the boolean to true. If no match, then i is incremented and the inner for loop is scanning from element 3 to n.
因此,您从第一个元素开始,然后从元素 2 扫描到 n(即列数)。如果找到匹配项,则将布尔值设置为 true。如果不匹配,则 i 递增并且内部 for 循环从元素 3 扫描到 n。
Follow a similar procedure for the columns, and you're done!
对列执行类似的过程,您就完成了!
回答by phatfingers
Let's say you create a method that operates on a single-dimensional array. You break the problem down into extracting strips of numbers from your 2d array into a 1d array. It's signature might look like boolean containsDupes(int[] strip) { ... }
.
假设您创建了一个对一维数组进行操作的方法。您将问题分解为将二维数组中的数字条提取为一维数组。它的签名可能看起来像boolean containsDupes(int[] strip) { ... }
.
There are a couple of approaches in that method that would make it easier to solve. One would be to sort that array so the dupes are next to each other. Another would be to populate a HashSet with each value and compare the size of the Set with the length of your array.
该方法中有几种方法可以使其更容易解决。一种方法是对该数组进行排序,使欺骗者彼此相邻。另一种方法是使用每个值填充 HashSet 并将 Set 的大小与数组的长度进行比较。
回答by spike
A more compact way of achieving this task is to add all the values to a Set and then compare the number of unique elements in the Set to the original row size using guava.
实现此任务的一种更紧凑的方法是将所有值添加到一个集合中,然后使用番石榴将集合中唯一元素的数量与原始行大小进行比较。
public static boolean hasDuplicates(int [][] inArray) {
for (int row = 0; row < inArray.length; row++) {
int curRow = inArray[row];
Set set = Sets.newHashSet(Arrays.asList(curRow));
if (set.size() < curRow.length) {
return true;
}
}
return false;
}