JAVA - How to find duplicate values ​​in rows and columns in a 2D array?

I have a 2D array and I would like to find an easier way to manipulate my code so that it detects if there is a duplicate in the column and an easier way than what I have below:

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;

EDIT: KNOWN ABOVE ^ ^ DOES NOT WORK AS IT WILL BE AGAINST EXCLUSION OF BOLONS

This method has too many loops, and I'm sure there should be an easier way to find duplicates, rather than going through this massive loop process.

This is for a square 2D array, i.e. array with rows = columns.

If so, how does this new way work - and how can I manipulate it to work to find duplicate values ​​in strings.

Thanks for the help.

+3
source share
3

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 ( ^ 2) = O (n), n - . , O, .

+3
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]
+1

Search for duplicate elements in a given matrix - 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]+" ");
            }
        }
    }
}
0
source

All Articles