Single Number II

Medium · Bit Manipulation · Array · Bit Manipulation

Problem

In the integer array nums, every value appears exactly three times except one value, which appears exactly once. Return that value.

Aim for linear time and constant extra space.

Examples

Input: nums = [5,5,-1,5]
Output: -1

5 appears three times and -1 once.

Input: nums = [9,1,9,1,9,1,4]
Output: 4

9 and 1 each appear three times, leaving 4.

Input: nums = [7]
Output: 7

Nothing repeats; 7 is the single value.

Constraints

Approaches

Follow-up

Counting each of the 32 bits separately and keeping count % 3 also works in O(32 n). The use cases change the repeat count to any k and ask for the value that appears twice instead of once.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…