Skip to content
BytePatterns

Single Value Among Triples

MediumBit Manipulation#bit-counting#modular-arithmetic~30m

Problem

In a list of non-negative whole numbers, every value appears exactly three times except one, which appears once. Find that lone value. Aim for constant extra space, so building a tally of every distinct value is off the table.

Examples

Input:  nums = [2, 2, 3, 2]
Output: 3
Input:  nums = [30, 1, 1, 1, 30, 30, 7]
Output: 7
Why:    the repeats do not have to sit together
Input:  nums = [5]
Output: 5
Why:    edge case, a single value is trivially the lone one

Hints

0 / 3

Stuck on the idea rather than the code? XOR Tricks covers it.