【问题标题】:JAVA - How to find duplicate values in rows and columns in a 2D Array?JAVA - 如何在二维数组的行和列中查找重复值?
【发布时间】:2011-09-01 14:43:34
【问题描述】:

我有一个 2D 数组,我想找到一种更简单的方法来操作我的代码,以便它会发现列中是否存在重复,并且比我下面的方法更简单:

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

编辑:请指出以上内容 ^^ 不会工作,因为它会抛出一个超出范围的异常

这种方式有太多的循环,我相信一定有一种更简单的方法来查找重复项,而不是经历这个庞大的循环过程。

这是一个方形二维数组,即。行 = 列的数组。

如果是这样,这种新方法如何工作 - 我如何操作它以找到行中的重复值。

感谢您的帮助。

【问题讨论】:

  • 不会数组[j+k][i] 为 k=j=length-1 抛出 OutOfBoundException 吗?
  • 哦,当然会。谢谢指出,一直没意识到。谢谢。
  • 您的要求不够明确。你想找到所有重复值的索引吗?您在哪里指定要查找的副本是什么?你为什么想知道这样的事情?我投票结束这个问题。
  • 我正在寻找行和列中是否存在重复值。这是一个需要这个的项目。

标签: java arrays loops for-loop duplicates


【解决方案1】:

您可以使用HashSet 来存储所有已经遇到的元素。应该是这样的:

static boolean noDupes(int[][] array) {
    for (int i=0; i < array.length; i++) {
        HashSet<Integer> set = new HashSet<Integer>();
        for (int j=0; j < array.length; j++) {
            if (set.contains(array[j][i])) return false;
            set.add(array[j][i]);
        }
    }
    return true;
}


此解决方案为 O(length^2) = O(n),其中 n 是矩阵总大小。我认为它在大 O 方面是理想的,因为您需要检查所有元素。

【讨论】:

  • (*)editted:if语句行中的混合索引,改变了array[i][j]->array[j][i]
  • 感谢您的好意。
【解决方案2】:
int[][] array = new int[3][5];

for (int i = 0; i < array.length; i++) // array initialization
  for (int j = 0; j < array[i].length; j++ )
    array[i][j] = i*j;

Map<Integer, Set<Point>> map = new HashMap<Integer, Set<Point>>();

for (int i = 0; i < array.length; i++)
  for (int j = 0; j < array[i].length; j++)
    if (map.containsKey(array[i][j]))
      map.get(array[i][j]).add(new Point(i, j));
    else
    {
      Set<Point> set = new HashSet<Point>();
      set.add(new Point(i, j));
      map.put(array[i][j], set);
    }


for (Map.Entry<Integer, Set<Point>> entry : map.entrySet())
  if (entry.getValue().size() > 1)
  {
    System.out.println("value = " + entry.getKey());
    for (Point p : entry.getValue())
      System.out.println("coordinates = " + p);
    System.out.println();
  }

输出如预期:

value = 0
coordinates = java.awt.Point[x=0,y=3]
coordinates = java.awt.Point[x=0,y=0]
coordinates = java.awt.Point[x=2,y=0]
coordinates = java.awt.Point[x=0,y=4]
coordinates = java.awt.Point[x=0,y=2]
coordinates = java.awt.Point[x=1,y=0]
coordinates = java.awt.Point[x=0,y=1]

value = 2
coordinates = java.awt.Point[x=1,y=2]
coordinates = java.awt.Point[x=2,y=1]

value = 4
coordinates = java.awt.Point[x=2,y=2]
coordinates = java.awt.Point[x=1,y=4]

【讨论】:

    【解决方案3】:

    在给定矩阵中查找重复元素 - JAVA

    static void findDuplicates(String[][] matrix) {
        HashSet<String> uniqInp = new HashSet<String>();
        HashSet<String> allDup = new HashSet<String>();
        System.out.println("***** DUPLICATE ELEMENTS *****");
    
        for(int row=0;row<matrix.length;row++) 
        {
            for(int col=0;col<matrix[0].length;col++)
            {
                if(uniqInp.add(matrix[row][col]))
                    //If not duplicate it will add
                    continue;
                else {
                    // If Duplicate element found, it will come here
                    if(allDup.add(matrix[row][col]))
                    System.out.print(matrix[row][col]+" ");
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-06-09
      • 1970-01-01
      • 2021-07-08
      • 2021-04-27
      • 1970-01-01
      • 2012-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-25
      相关资源
      最近更新 更多