0

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!

WizKid22
  • 23
  • 2
  • 5
  • @KaustubhKhare true that it's dup, but there's no good answer neither there nor in the dup that's linked from there – Nir Alfasi Nov 13 '17 at 05:27
  • Yes, but in the linked question I dont really know what is going on in the top answer. – WizKid22 Nov 13 '17 at 05:35
  • According to your edit it seem that the 2D array should contain all the values 1...n^2 without duplicates (each number should appear exactly once). Now that's a different problem, can you use another array as an accessory ? – Nir Alfasi Nov 13 '17 at 17:14

1 Answers1

0

You can simplify the logic by saving the numbers into a hashset and when you're iterating them check each one if it's already there (meaning duplicate) otherwise add it:

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

EDIT

We can use an array for the same functionality the hashset is doing in the code above:

public static boolean correctNumbers(int[][] values) {
    int n = values.length;
    int[] dict = new int[n * n + 1];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < values[i].length; j++) {
            int num = values[i][j];
            if (num < 1 || num > n || dict[num] != 0) {
                return false;
            }
            dict[num] = 1;
        }
    }
    return true;
}
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • thanks for the answer, but do you know of any way to do it without using a hashset? – WizKid22 Nov 13 '17 at 05:40
  • @WizKid22 any reason to *not* use a hashset ? – Nir Alfasi Nov 13 '17 at 16:07
  • Because it is for a class that I have. Later this week we will have a test, and the professor has provided us with sample questions as a study guide. I was going through them and got stuck on this question, and in this class we have not yet learned how to use hashsets, so im not sure if it would be allowed on the test. – WizKid22 Nov 13 '17 at 22:21
  • @WizKid22 can you use an additional array? – Nir Alfasi Nov 13 '17 at 23:26
  • yes I can use an additional array – WizKid22 Nov 14 '17 at 01:30
  • @WizKid22 then check out the "edit" section I've just added – Nir Alfasi Nov 14 '17 at 01:36
  • thank you for the input. I edited your answer to a new code I made that seems to work. I explained what I wanted poorly probably. Let me know what you think. Im sure there is a more efficient way to do it, while still using an array like this. – WizKid22 Nov 14 '17 at 03:31
  • @WizKid22 please do not edit the answer in order to comment. If you want to comment you should use the comment-section. – Nir Alfasi Nov 14 '17 at 03:32
  • ah I am new to stackoverflow sorry, do not know how things work around here. My apologies. Should I put my new code into a separate answer? – WizKid22 Nov 14 '17 at 03:33
  • No worries - it's good that you're asking for the proper way: you should update your question - add a new section, call it "update" or something like that. – Nir Alfasi Nov 14 '17 at 03:38
  • Alright will do, thanks for the help. – WizKid22 Nov 14 '17 at 04:08