Skip to main content
Back to problems
Leetcode
Medium
Arrays
Bit Manipulation
Google
Single Number II

Find the one integer that appears exactly once when every other integer appears three times.

Acceptance 0%
Problem Statement

Problem

You are given an integer array nums where every value appears exactly three times, except for one value that appears exactly once.

Return the value that appears only once.

You should aim for a solution that uses constant extra space and runs in linear time.

Notes

  • The array may contain negative numbers.
  • The unique value is guaranteed to exist.
  • All non-unique values appear exactly three times.

Input Format

  • One integer array nums.
  • nums[i] is an integer value.
  • Every element appears three times except one element that appears once.

Output Format

  • Return the integer that appears exactly once.

Constraints

  • 1nums.length1 \le nums.length
  • All values except one occur exactly 3 times.
  • The remaining value occurs exactly 1 time.
  • Try to use O(1)O(1) extra space.
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2,2,3,2]

Output

3

Explanation

Every number appears three times except 3, which appears once.

Example 2

Input

nums = [0,1,0,1,0,1,99]

Output

99

Explanation

0 and 1 each appear three times, while 99 appears once.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.