Skip to main content

Single Number II

MediumArrayMathBit Manipulation

Description

Given an array where every element appears three times except one, find that single one. Use bit manipulation for O(1) space.

Examples

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

3 appears once, 2 appears three times.

Input:nums = [5,7,5,4,7,4,4,5,7]
Output:0
Explanation:

In this array, 5 appears three times and 7 appears three times, but 4 appears only once. Therefore, 4 is the single number that doesn't follow the pattern of appearing three times.

Input:nums = [-1]
Output:-1
Explanation:

This is a minimal case where the array contains only one element. Since there's only one number and it doesn't appear three times, -1 is the single number is being looking for.

Constraints

  • 1 ≤ nums.length ≤ 3 * 10⁴
  • -2³¹ ≤ nums[i] ≤ 2³¹ - 1

Ready to solve this problem?

Practice solo and sharpen your skills for technical interviews.