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.
-
won't array[j+k][i] throw OutOfBoundException for k=j=length-1 ?amit– amit2011年05月25日 09:39:44 +00:00Commented May 25, 2011 at 9:39
-
Oh it will, of course. Thanks for pointing it out, never realised that. Thanks.ron8– ron82011年05月25日 09:42:00 +00:00Commented May 25, 2011 at 9:42
-
Your requirements aren't clear enough. Do you want to find the indexes of all the duplicate values? Where do you specify what the duplicate you're looking for is? And why would you want to know such a thing? I'm voting to close the question.duffymo– duffymo2011年05月25日 09:50:30 +00:00Commented May 25, 2011 at 9:50
-
I am looking to find if there are repeated values in the rows and columns. This is for a project that requires this.ron8– ron82011年05月25日 10:10:04 +00:00Commented May 25, 2011 at 10:10
3 Answers 3
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.
-
(*)editted: mixed indexes in the if statement line, changed array[i][j]->array[j][i]amit– amit2011年05月25日 09:55:43 +00:00Commented May 25, 2011 at 9:55
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]
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]+" ");
}
}
}
}
Explore related questions
See similar questions with these tags.