I'm trying to complete the Hackerrank Sales by Match problem:
www [dot] hackerrank [dot] com/challenges/sock-merchant/problem?isFullScreen=true
My method is meant to just look for duplicates, remove them, then call itself to look for another duplicate. If there are no duplicates, then it just returns the number of times it found a duplicate.
I added a bunch of sysout lines to help debug, and according to the debug output, I am successfully getting the count variable to correctly identify the number of duplicates. Here are my problems:
1. Why do I need the return statement at the end? And how is my code even reaching it? When the if statement is true, it reaches a return statement. When the if statement is false, the method just calls itself again. So that last return statement at the end should never be reached, but somehow it is being reached and returning a 1.
2. See my debug output below the code. Why is my for loop continuing to run even after the if statement is true (and therefore my method reaches a return statement and should stop)?
-----if it makes a difference, when I run it against the other test case, the method actually stops when there are no more duplicates, but still returns a 1 instead of the actual count (as if it's reaching the return statement at the end of the code somehow).
class Result { /* * Complete the 'sockMerchant' function below. * * The function is expected to return an INTEGER. * The function accepts following parameters: * 1. INTEGER n * 2. INTEGER_ARRAY ar */ public static int sockMerchant(int n, List<Integer> ar) { return matchCheck(ar, 0); } public static int matchCheck(List<Integer> ar, int newCount){ ar.sort(Comparator.naturalOrder()); System.out.println("matchCheck method has been called!"); TreeSet set = new TreeSet<Integer>(ar); System.out.printf("TreeSet is length %d: ", set.size()); System.out.println(set); System.out.printf("ArrayList is length %d: ", ar.size()); System.out.println(ar); if (set.size() == ar.size()) { System.out.println("TreeSet and ArrayList are equal in size"); return newCount; } else { System.out.println(set.size() == ar.size()); for (int i = 0; i < ar.size() - 1; i++) { for (int j = i + 1; j < ar.size(); j++) { System.out.printf("Checking %d and %d%n", ar.get(i), ar.get(j)); if (ar.get(i) == ar.get(j)) { System.out.printf("%d and %d are a match on index %d and %d%n", ar.get(i), ar.get(j), i, j); newCount++; ar.remove(j); ar.remove(i); System.out.println("Count is " + newCount); matchCheck(ar, newCount); } } } } return newCount; } }
The debug output is as follows:
matchCheck method has been called! TreeSet is length 4: [10, 20, 30, 50] ArrayList is length 9: [10, 10, 10, 10, 20, 20, 20, 30, 50] false Checking 10 and 10 10 and 10 are a match on index 0 and 1 Count is 1 matchCheck method has been called! TreeSet is length 4: [10, 20, 30, 50] ArrayList is length 7: [10, 10, 20, 20, 20, 30, 50] false Checking 10 and 10 10 and 10 are a match on index 0 and 1 Count is 2 matchCheck method has been called! TreeSet is length 3: [20, 30, 50] ArrayList is length 5: [20, 20, 20, 30, 50] false Checking 20 and 20 20 and 20 are a match on index 0 and 1 Count is 3 matchCheck method has been called! TreeSet is length 3: [20, 30, 50] ArrayList is length 3: [20, 30, 50] TreeSet and ArrayList are equal in size Checking 20 and 50 Checking 30 and 50 Checking 20 and 50 Checking 30 and 50 Checking 20 and 50 Checking 30 and 50