Single Number II
Pattern: XOR tricks
Count each bit modulo 3 across all values. Two masks hold the count for every bit at once, ones (count 1) and twos (count 2). A bit that reaches 3 resets in both, so after the pass ones holds exactly the single value's bits.
The problem
Every value in an array appears exactly three times, except one value that appears once. Return that value in O(n) time
and O(1) extra memory. For example, [2, 2, 3, 2] gives 3 and [0, 1, 0, 1, 0, 1, 99] gives 99.
The function name singleNumber matches LeetCode. Use the link above for the exact statement and limits.
Think first
- Why does plain XOR fail here?
- For one bit position, how many times is it set across the array, modulo 3?
- Can you track that count for all 32 bits at once?
Hints
Brute force, and why it fails
A hash map of counts is O(n) memory. Summing each of the 32 bits separately and taking mod 3 is O(32n) time and O(1) memory. The two-mask method does all 32 bits in one pass.
The key insight
Each bit is a small counter that goes 0, 1, 2, 0. Two masks store the state of all 32 counters at once, and two lines of bit logic advance them all together.
Dry run
| ones | 00 |
|---|---|
| twos | 00 |
nums = [2, 2, 3, 2]. In binary 10, 10, 11, 10. Start with ones = 0 and twos = 0.
The plan
ones = 0, twos = 0
for v in nums:
ones = (ones ^ v) & ~twos
twos = (twos ^ v) & ~ones
return ones
Complexity
- Time
O(n)- Space
O(1)
One pass with two masks.
Edge cases
- One element: itself.
- Negative values, including -2³¹: the sign bit is counted like any other bit.
- The single value is 0.
I've built my logic, show the code
Java
class Solution {
public int singleNumber(int[] nums) {
int ones = 0, twos = 0; // bits seen once (mod 3), bits seen twice (mod 3)
for (int v : nums) {
ones = (ones ^ v) & ~twos; // add v to "once" unless that bit is already at "twice"
twos = (twos ^ v) & ~ones; // add v to "twice" unless it just landed in "once"
}
return ones; // bits seen 3 times have cleared; the single value is left
}
}C++
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ones = 0, twos = 0; // bits seen once (mod 3), bits seen twice (mod 3)
for (int v : nums) {
ones = (ones ^ v) & ~twos; // add v to "once" unless that bit is already at "twice"
twos = (twos ^ v) & ~ones; // add v to "twice" unless it just landed in "once"
}
return ones; // bits seen 3 times have cleared; the single value is left
}
};Python
class Solution:
def singleNumber(self, nums: list[int]) -> int:
ones = twos = 0 # bits seen once (mod 3), bits seen twice (mod 3)
for v in nums:
ones = (ones ^ v) & ~twos # add v to "once" unless that bit is already at "twice"
twos = (twos ^ v) & ~ones # add v to "twice" unless it just landed in "once"
return ones # bits seen 3 times have cleared; the single value is leftCommon mistakes
- Updating
twosbeforeones, which breaks the state machine. - Using plain XOR, which returns the XOR of all values instead.
- In Python, assuming fixed 32-bit width; the masks still work because they never grow past the input bits.