The Two Sneaky Numbers of Digitville (LeetCode 3289): Solution and Explanation

Easy
Array
Hash Table
Math

The list nums should hold each of 0..n-1 once, but two values were added a second time, so it has n + 2 elements. Return the two values that appear twice, in any order.

Intuition

Every value is a small non-negative integer below n. That means the values themselves can serve as array indexes, which is the cheapest possible hash table.

Scan the list once and add one to count[x] for every element x. Honest values end with a count of 1 and the two sneaky ones end with a count of 2.

A second, shorter sweep over the n counters picks out the two values with a count of 2. Sweeping in value order also makes the output come out sorted, which is a tidy deterministic order even though any order is accepted.

Approach: Count array indexed by value

  1. Let n = len(nums) - 2 and create count, an array of n zeros.
  2. For every x in nums, increment count[x].
  3. Sweep v from 0 to n - 1. Whenever count[v] == 2, append v to the result.
  4. Return the result, which always has exactly two values.

Step-by-step example

nums = [0,1,1,0]
  1. There are 4 numbers, so n = 2 and the bins are count[0] and count[1].
  2. 0 goes into bin 0 (count[0] = 1), then 1 into bin 1 (count[1] = 1).
  3. The second 1 makes count[1] = 2, and the second 0 makes count[0] = 2.
  4. The sweep finds count[0] = 2, then count[1] = 2, so the answer is [0,1].

Code: Python, Java, C++

1class Solution:
2    def getSneakyNumbers(self, nums: List[int]) -> List[int]:
3        n = len(nums) - 2
4        count = [0] * n
5        for x in nums:
6            count[x] += 1
7        result = []
8        for v in range(n):
9            if count[v] == 2:
10                result.append(v)
11        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Count array indexed by value O(n)
Hash set O(n)
XOR split O(n)
Sort and compare neighbors O(n log n)
Time

O(n)

One pass over the n + 2 elements fills the counters and one pass over the n counters reads them.

Space

O(n)

The count array has one slot per possible value.

Other approaches

Hash set (time O(n), space O(n))

Add each value to a set and record it when it is already present. It works for any value range but uses a real hash table where a plain array is enough.

XOR split (time O(n), space O(1))

XOR every element and every value 0..n-1 together to get a ^ b, then split all numbers by the lowest set bit of that value and XOR each group to recover a and b. It uses O(1) extra space, the same trick as Single Number III.

Sort and compare neighbors (time O(n log n), space O(n))

Sort a copy of the list and report every value equal to its neighbor. Simple, but slower than counting.

Common mistakes

  • Sizing the count array as len(nums) and then sweeping too far. The values only go up to n - 1, where n is the length minus 2.
  • Adding a value to the answer every time it is seen again. Each sneaky value appears exactly twice here, but counting and sweeping afterwards stays correct even when you reuse the idea for other limits.
  • Assuming the two sneaky numbers are adjacent in the list. They can be anywhere.

Frequently asked questions

Does the output order matter?

No. Any order is accepted. Sweeping the counters from 0 upward simply produces the two values in increasing order.

Can this be done without extra memory?

Yes. XOR all elements with 0..n-1 to get the XOR of the two sneaky values, then split by any set bit, as in Single Number III. You can also mark seen values by negating or offsetting entries in place if you are allowed to modify the input.

Why does a plain array work as the hash table?

Every value is between 0 and n - 1, so it maps directly to an index with no collisions and no hashing.