Single Number II
Given an integer array nums where every element appears exactly three times except for one element that appears exactly once, return the single element. Solve it in linear time and constant extra space.
Open official problem prompt ↗Find the one value appearing a single time while every other value appears three times, without extra memory.
A three-state turnstile per bit: each time a bit passes, the counter advances 00 -> 01 -> 10 and snaps back to 00 on the third pass, so only bits seen a non-multiple-of-three number of times stay lit.
- Input
- nums = [0, 1, 0, 1, 0, 1, 99]
- Output
- 99
- Why
- 0 and 1 each appear three times and cancel under mod-3 bit counting; 99 is the lone value.
1 <= nums.length <= 3 * 10^4-2^31 <= nums[i] <= 2^31 - 1Each element appears exactly three times except one which appears onceMust run in O(n) time and O(1) extra space