Problem 478
Single Number II
Medium · 20 pts · Array, Bit Manipulation
Problem
Every element of the array appears exactly three times, except for one element that appears exactly once. Find that element. A hash map works, but there is a neat O(1)-memory solution: for each bit position, count how many numbers have that bit set; the count modulo 3 is the bit of the single number. Negative numbers are allowed.
Input
First line: N. Second line: N integers.
Output
Print the element that appears once.
Constraints
1 <= N <= 30000; -2147483648 <= A[i] <= 2147483647
Example input
4 2 2 3 2
Example output
3
Explanation
In 2 2 3 2, the value 2 appears three times and 3 appears once, so the answer is 3.