Given an integer array nums where every element appears exactly three times except for one element, which appears exactly once, find that single element and return it. The solution must run in linear time and use only constant extra space.
Given an integer array nums where every element appears exactly three times except for one element, which appears exactly once, find that single element and return it. The solution must run in linear time and use only constant extra space.
Example 1:
Input: {"nums":[2,2,3,2]}
Output: 3
Constraints:
1≤nums.length≤3×104
−231≤nums[i]≤231−1
Each element in nums appears exactly three times except for one element which appears once.
Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result
Run code to see results.
Input
arr ={"nums":[2,2,3,2]}
Every bit column counts how many 1s it has seen, modulo 3. Two registers hold that count: a bit in ones means count 1, a bit in twos means count 2.