Single Number II

Implement singleNumber

This time the pattern is triples, not pairs — every value in the array repeats exactly three times except for one lone holdout that shows up just once. Identify it. Plain XOR can't help here, since XOR-ing a value with itself three times just leaves the value unchanged (it doesn't cancel like it does for pairs). Instead, look at each of the 32 bit positions on its own: a value repeated three times always contributes a multiple of 3 to that position's total, so any position whose total isn't a multiple of 3 must be getting its extra contribution from the one unpaired value.

Example 1:

Input: nums = [2,2,3,2]

Output: 3

Example 2:

Input: nums = [0,1,0,1,0,1,99]

Output: 99

Example 3:

Input: nums = [5,5,5,7]

Output: 7

+ 10 hidden test cases run on Submit.

Constraints:

  • 1 ≤ nums.length ≤ 3 × 10⁴
  • -3 × 10⁴ ≤ nums[i] ≤ 3 × 10⁴
  • Every value in nums appears exactly three times, except for one value which appears exactly once

nums =

[2, 2, 3, 2]