MediumEditorial · 7 minGenerated by the editor · Nov 2
Open — the attempt gate is not wired up yet
This editorial is meant to unlock after you have run the problem at least once, with the worked solution behind one further deliberate click. That needs per-learner unlock state nothing stores today, so for now the whole article is open.
Intuitionwhy three-state bit tracking removes every triplet
Look at one bit position at a time. Across the whole array, that bit is present once for the unique value and a multiple of three times for every repeated value. Therefore its total count modulo three is zero for repeated values and one for the answer. If you could compute that remainder independently for all 32 bit positions, the remaining bits would reconstruct the single integer.
The constant-space trick is to avoid storing 32 counters. For each bit, keep only its count modulo three using two bits of state: 00 means seen zero times, 01 means seen once, and 10 means seen twice. Seeing the bit again moves 10 back to 00. The variables ones and twos store these states for all bit positions in parallel, so one integer operation updates all 32 counters at once.
Each bit is a tiny counter modulo three, packed into the two masks.
Approach
1Start ones and twos at zero, because no bit has been seen before the scan begins and the state 00 represents zero occurrences.
2For each value x, update ones with (ones ^ x) & ~twos. XOR toggles every bit affected by x, while ~twos prevents a bit already in the two-occurrence state from entering ones on the wrong transition.
3Update twos with (twos ^ x) & ~ones, using the newly updated ones. This records bits moving into the two-occurrence state and clears bits that have just completed their third occurrence.
4Scan the entire array once. The two masks process all bit positions together, so the loop does not need a separate counter or a second pass.
5Return ones after the scan. Every repeated value contributes three appearances and returns its bits to state 00; the unique value contributes once and leaves exactly its bits in ones.
Complexityconstant working memory despite 32 independent bit counters
MEASURE
BOUND
WHY
Time
O(n)
The loop performs a fixed number of bitwise operations for each of the n values. Each operation handles all W = 32 bit positions together, so no bit position causes another scan of the array.
Space
O(1) extra
Only the two integer masks and a loop variable are stored; their size does not grow with n. The returned integer is required output and is excluded from the working-space bound, which remains constant even for the worst allowed input shape.
Here n is the number of array elements and W = 32 is the fixed width of the integer representation. Since W is fixed, O(n x W) is O(n).
Annotated solutionC++ · two-mask finite-state counter · constant extra space
CPPThe two masks encode each bit's occurrence count modulo three and return the bits left in the one-occurrence state.
#include <vector>
using namespace std;
class Solution {
public:
int singleNumber(vector<int>& nums) {
int ones = 0;
int twos = 0;
for (int x : nums) {
ones = (ones ^ x) & ~twos;
twos = (twos ^ x) & ~ones;
}
return ones;
}
};
The order of the two assignments is part of the algorithm. After ones is updated, it already describes the new one-occurrence state, so twos must use that new value to remove bits that have just moved out of state one. The masks act independently at every bit position, but the same pair of integer operations updates all positions simultaneously.
Common mistakesthe state transition is compact, but not interchangeable