ron8
ron8

Reputation: 243

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 will find if there is a duplicate in the column and easier way then 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: KINDLY POINTED OUT THE ABOVE ^^ WON'T WORK EITHER AS IT WILL THROW AN OUT OF BOUNDS EXCEPTION

This way has too many loops and I am sure there must be an easier way to find duplicates rather than going through this massive looping process.

This is for a square 2D array, ie. an array with rows = columns.

If so, how can this new way work - and how can I manipulate it to work to find duplicate values in the rows as well.

Thanks for the help.

Upvotes: 3

Views: 9847

Answers (3)

Manish Kumar Majumdar
Manish Kumar Majumdar

Reputation: 21

Finding the 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]+" ");
            }
        }
    }
}

Upvotes: 0

Boris Pavlović
Boris Pavlović

Reputation: 64632

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();
  }

The output is as expected:

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]

Upvotes: 1

amit
amit

Reputation: 178461

you can use HashSet to store all already encountered elements. should be something like this:

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;
}


this solution is O(length^2) = O(n) where n is the matrix total size. I think it is ideal in terms of big O, because you need to check all elements.

Upvotes: 3

Related Questions