Single Number II

Find the unique element when others appear three times.

Problem: Every element appears three times except one. Find it.

Approach: Count bits. For each bit position, sum the bits across all numbers. If divisible by 33, the unique element has 00 there. Otherwise, it has 11.

function singleNumber(nums):
    result = 0
    for i from 0 to 31:
        bitSum = 0
        for num in nums:
            bitSum = bitSum + ((num >> i) & 1)
        if bitSum mod 3 != 0:
            result = result | (1 << i)
    return result

Time: O(32n)=O(n)O(32n) = O(n).

Space: O(1)O(1) (excluding input array).

Alternative: Use two variables to simulate a base-33 counter per bit.