Single Number III

Medium

Topics
ArrayBit Manipulation

Given an array nums in which exactly two elements appear only once and all the others appear exactly twice, return the two single numbers sorted in ascending order. Use O(n) time and O(1) extra space.

Example 1

nums121325
76543210xor all00000110lowest set bit00000010
bit setbit clear
XORing everything gives 6, which is the XOR of the two unpaired values — so any bit set in it is a bit where they differ. Splitting the array by its lowest set bit puts one answer in each group and every pair wholly inside one group, reducing this to the single-number problem twice: 3 and 5.
Top row is the input, split by the chosen bit; below it the XOR of everything and the bit used to split.
Input:  nums = [1,2,1,3,2,5]
Output: [3,5]

Example 2

nums01
76543210xor all00000001lowest set bit00000001
bit setbit clear
Input:  nums = [0,1]
Output: [0,1]

Constraints

  • 2 <= nums.length <= 3*10^4
  • Exactly two elements appear once; the rest appear twice.
Editorial — intuition, approach, complexity and pitfalls

Intuition

XOR the whole array and the paired values cancel, leaving a ^ b for the two singletons. That is not yet an answer, but it is enough to separate them: since a != b, their XOR has at least one set bit, and at any such bit the two values differ.

Pick one — the lowest, via x & -x — and use it to split the array into two groups. Every paired value lands wholly in one group, so XOR-ing within each group cancels the pairs and leaves one singleton each. Two passes, no extra space.

Approach

  1. XOR everything to get x = a ^ b.
  2. Isolate the lowest set bit with diff = x & -x.
  3. Partition on that bit, XOR-ing each group separately.
  4. Return the two results in ascending order.

Dry Run

nums = [1,2,1,3,2,5]

xor of everything  = 6 = 0b110      which is 3 ^ 5
lowest set bit     = 2 = 0b10       partition on bit 1

  bit set    [2, 3, 2]   xor = 3
  bit clear  [1, 1, 5]   xor = 5

answer [3, 5]

Complexity

  • Time — O(n): two linear passes.
  • Space — O(1): a handful of integers. The trailing two-element sort is a fixed comparison, not a sort over the input, so the constant-space claim survives it.

Common Pitfalls

  • Using the highest set bit, or any arbitrary bit of x. Any set bit works; x & -x is just the cheapest to compute. Picking a bit that is clear in x does not work, because then both singletons fall in the same group.
  • Writing x & (x - 1) instead of x & -x. That clears the lowest set bit rather than isolating it.
  • Forgetting to sort the pair when the problem asks for ascending order. Here it does.
  • Using a hash set and removing seen values. O(n) time but O(n) space, and the problem names O(1).
  • Trying to recover a and b from a ^ b alone. One equation, two unknowns — the partition is what supplies the second equation.
  • In a fixed-width language, computing -x on the minimum integer. It overflows to itself, which happens to give the right mask, but the reasoning deserves a sentence rather than luck.

Full Solution — all languages

class Solution:
    def singleNumber(self, nums):
        x = 0
        for v in nums:
            x ^= v
        diff = x & (-x)
        a = b = 0
        for v in nums:
            if v & diff:
                a ^= v
            else:
                b ^= v
        return sorted([a, b])
Asked at Zomato
SensAI
Beta
Tap the mic to stop voice mode