I am a beginner coder just learning arrays. I recently learned 2d arrays and I am trying to find out if there is a duplicate in a 2d array. I know how to check if there is a duplicate in the same column or row, but I cannot figure out how to compare a number if it is not in the same column or row as the number I am trying to compare it to. Here is my code as of now:
public static boolean correctNumbers(int[][] values) {
for (int i = 0; i < values.length; i++) {
for (int j = 0; j < values[i].length; j++) {
int num = values[i][j];
for (int k = j + 1; k < values.length; k++) {
if (num == values[i][k] || num > values.length * values.length || num < 1) {
return false;
}
}
for (int l = i + 1; l < values.length; l++) {
if (num == values[l][j] || num > values.length * values.length || num < 1) {
return false;
}
}
}
}
return true;
}
I need to create a method and I cannot use any other methods in creating it. Thanks for the help!
Edit: It returns false if there is duplicates in the array, a number in the array is less than 1, or greater than total number of elements in the array. In other words this method is checking to see if the array contains all the values of 1 to (i*j) in the array. I realized I did a bad job of explaining that part. Thanks again!