Are duplicates allowed?

  • 0

    The problem description only mentions that the answer is unique, but it doesn't force the numbers to be unique. However, if there are exactly two equal numbers that add up to the target, then it is possible for the solution to be unique even though the numbers are not. The simplest test I can think of is [1, 1], 2.

    But then the approach #2 from the editorial solution is incorrect:

        if (map.containsKey(complement) && map.get(complement) != i) {
            return new int[] { i, map.get(complement) };

    It doesn't even allow for duplicates. Interestingly enough, the approach #3 is fine because instead of checking for duplicates, it adds the index to the map after checking for the complement.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.