You have an array where every number appears exactly three times, except one number that appears once. The task is to find that single number.
In the classic Single Number problem (LeetCode 136), every duplicate appears twice, so XOR of all elements cancels the pairs and leaves the unique one. XOR cancels in pairs, not triples, so that technique does not transfer here.
The problem is how to cancel out elements that appear three times. The approaches below go from counting occurrences directly to a bit manipulation method that processes each bit position independently and meets the O(1) space requirement.
1 <= nums.length <= 3 * 10^4: With n up to 30,000, O(n^2) means roughly 900 million operations, which is borderline but likely too slow. O(n log n) is comfortable, and O(n) is ideal.-2^31 <= nums[i] <= 2^31 - 1: Full 32-bit integer range, including negatives. Any bit-level approach needs to handle negative numbers properly (32 bits, two's complement).Count how many times each number appears, then return the one with a count of 1. A hash map records the counts in a single pass.
This does not meet the O(1) space requirement, but it gives a correct baseline that the bit-manipulation approaches can be measured against.
The hash map approach meets the O(n) time requirement but uses O(n) extra space. The next approach reaches the answer without storing every element, by working one bit position at a time.
Instead of working with whole numbers, work with each bit position independently.
Take a single bit position, say bit 0 (the least significant bit). Across all elements, each number that appears three times contributes either 0 or 3 to the count of 1s at that position. Either way, the contribution from tripled elements is divisible by 3.
The single number's bit at that position adds either 0 or 1 to the total. So if we take the total count of 1s at bit position 0 and compute count % 3, the remainder tells us what the single number's bit is at that position.
We repeat this for all 32 bit positions, and we reconstruct the single number bit by bit.
Negative numbers need no special handling, because the count operates on the raw bit representation. In two's complement, the sign bit (bit 31) is one more bit position, and the same mod-3 logic applies. Reconstructing a number with bit 31 set produces a negative value in a signed 32-bit type, which is the result wanted.
The bit-by-bit approach achieves O(n) time and O(1) space, but it makes 32 passes over the array. The next approach processes all 32 bits in a single pass using two bitmasks.
Instead of counting bits one position at a time, process all 32 bits in parallel during a single pass, using a mod-3 counter that operates on entire integers at once.
Each bit position needs a counter that cycles through 0, 1, 2, and back to 0. Representing three states (0, 1, 2) takes two bits, so two variables hold them: ones and twos.
For each bit position independently:
ones = 0, twos = 0 means that bit has been seen 0 times (mod 3)ones = 1, twos = 0 means that bit has been seen 1 time (mod 3)ones = 0, twos = 1 means that bit has been seen 2 times (mod 3)The update formulas are:
ones = (ones ^ num) & ~twostwos = (twos ^ num) & ~onesThe XOR toggles bits as if we were doing a simple count. The AND with the complement acts as a mask that prevents a bit from being set in the wrong variable, implementing the three-state cycle. After processing all elements, ones holds the bits that appeared exactly once (mod 3), which is our answer.
After processing all numbers, every tripled element's bits have cycled back to (0,0), so ones holds only the single number's bits, in state (1,0).
The two update lines are not interchangeable. The first line updates ones, and the second line masks twos with the already updated ones. This guarantees ones and twos never both carry a 1 at the same bit position, which is what keeps the three-state cycle valid. Swapping the lines, or computing both from the old values, breaks the invariant and gives a wrong answer.
ones = 0 and twos = 0.ones: XOR with the number, then mask out any bits that are in twos.twos: XOR with the number, then mask out any bits that are in ones.ones contains the single number.ones.