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 , the unique element has there. Otherwise, it has .
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: .
Space: (excluding input array).
Alternative: Use two variables to simulate a base- counter per bit.