Skip to main content
Back to problems
Leetcode
Easy
Arrays
Bit Manipulation
Single Number

Find the one element that appears exactly once when every other element appears twice.

Acceptance 0%
Problem Statement

Problem

Given an integer array nums, every element appears exactly twice except for one element which appears only once.

Return the element that appears once.

You should aim for a solution with linear time complexity and constant extra space.

Input Format

  • An integer array nums
  • Each value appears twice except for one distinct value

Output Format

  • Return the single element that appears only once

Constraints

  • 1 <= nums.length
  • Exactly one value appears once
  • Every other value appears exactly twice
  • Use integer values that fit in standard 32-bit signed integer ranges

Hints

  • Think about an operation that cancels out equal values.
  • Try to avoid using extra memory proportional to the array size.

Input Format

  • An integer array nums
  • Exactly one number appears once; all others appear twice

Output Format

  • Return the unique number that appears once

Constraints

  • Exactly one value appears once
  • All other values appear exactly twice
  • Target linear time and constant extra space
Examples
Sample cases returned by the problem API.

Example 1

Input

nums = [2,2,1]

Output

1

Explanation

All numbers appear twice except 1, so the answer is 1.

Example 2

Input

nums = [4,1,2,1,2]

Output

4

Explanation

1 and 2 each appear twice, leaving 4 as the unique value.

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.