Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Hackerrank Sales by Match problem - I'm getting the answer but can't return it

  1. #1
    Junior Member
    Join Date
    Dec 2022
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hackerrank Sales by Match problem - I'm getting the answer but can't return it

    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
    Last edited by chazwinter; December 1st, 2022 at 11:03 PM.

  2. #2
    Junior Member
    Join Date
    Mar 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hackerrank Sales by Match problem - I'm getting the answer but can't return it

    When the method called itself in the code, it never returns its value at the end of its method execution, and the value for the newCount is lost.

    add the return statement to the matchCheck method. This should do the trick

Similar Threads

  1. Count Triplets Hackerrank
    By camicode in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 19th, 2019, 08:41 AM
  2. Replies: 3
    Last Post: June 26th, 2013, 02:12 AM
  3. Replies: 3
    Last Post: March 9th, 2013, 07:22 PM