LeetCode Problem

How to Solve Single Number

The Single Number problem requires finding the unique element in an array where every other element appears twice. Use bit manipulation, specifically XOR, to achieve linear time complexity and constant space usage. This approach efficiently solves the problem within the given constraints.

GhostInterview Help

Need help with Single Number without spending extra time grinding it?

GhostInterview can read Single Number from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #136Array plus Bit ManipulationReviewed 2026-03-08
Difficulty
Easy
Primary pattern
Array plus Bit Manipulation
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

The Single Number problem requires finding the unique element in an array where every other element appears twice. Use bit manipulation, specifically XOR, to achieve linear time complexity and constant space usage. This approach efficiently solves the problem within the given constraints.

Problem Statement

Given an array where every element appears twice except for one, your task is to find the single element that only appears once. You are required to implement a solution with linear runtime and constant extra space. The problem challenges you to solve it efficiently without using extra memory for sorting or hashing.

For example, consider an array such as nums = [2,2,1]. The output is 1 because it is the only number that appears once, while the other number appears twice. The array length is constrained between 1 and 30,000, and elements range between -30,000 and 30,000, with the unique number appearing only once in the array.

Examples

Example 1

Input: nums = [2,2,1]

Output: 1

Example details omitted.

Example 2

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

Output: 4

Example details omitted.

Example 3

Input: nums = [1]

Output: 1

Example details omitted.

Constraints

  • 1 <= nums.length <= 3 * 104
  • -3 * 104 <= nums[i] <= 3 * 104
  • Each element in the array appears twice except for one element which appears only once.

Solution Approach

XOR Operation

The key to solving this problem is using the XOR (^) operator. XOR has a unique property where x ^ x = 0, and x ^ 0 = x. This allows us to cancel out all the numbers that appear twice, leaving only the number that appears once.

Iterating Through the Array

To solve the problem, iterate through the array and apply the XOR operation to each element. This guarantees that after processing all elements, only the unique element remains, as the duplicates will cancel out.

Constant Space Usage

The XOR operation allows the solution to be implemented with constant space, as we only need one variable to store the cumulative XOR result. This meets the problem’s requirement to use O(1) extra space.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity of this solution is O(n), where n is the number of elements in the array. Each element is processed once, and the XOR operation runs in constant time. The space complexity is O(1), as only a single variable is used to store the XOR result, regardless of the input size.

What Interviewers Usually Probe

  • The candidate shows an understanding of bitwise operations, especially XOR, and how it can be used to efficiently solve problems with unique constraints.
  • The candidate provides an optimal solution that minimizes both time complexity and space usage, which is crucial for scalability in large datasets.
  • The candidate demonstrates the ability to understand and apply problem-specific constraints, such as the need for constant space and linear runtime.

Common Pitfalls or Variants

Common pitfalls

  • A common mistake is to use extra space for storing values or counting occurrences (e.g., using a hash map or sorting), which violates the constant space constraint.
  • Failing to recognize that XOR is the most efficient way to solve this problem can lead to slower, suboptimal solutions that use extra time and space.
  • Not understanding the properties of XOR can lead to incorrect logic, such as assuming that XOR only works on pairs of identical numbers instead of all duplicates.

Follow-up variants

  • Handling arrays with negative numbers, ensuring the XOR approach works regardless of sign.
  • Solving the problem with an alternative bit manipulation technique, such as using bitwise AND/OR to achieve similar results.
  • Working with arrays of varying sizes, from small arrays to large datasets, while maintaining linear time and constant space.

How GhostInterview Helps

  • GhostInterview assists by providing a detailed step-by-step explanation of the XOR operation and its role in efficiently solving the Single Number problem.
  • The platform ensures a focus on optimizing the solution to meet both time and space constraints, offering feedback on common mistakes and providing an optimal approach.
  • With GhostInterview, candidates can practice bit manipulation techniques, reinforcing the application of XOR and ensuring they can implement the solution under interview conditions.

Topic Pages

FAQ

What is the optimal approach for solving the Single Number problem?

The optimal approach is using the XOR operator, which allows cancellation of duplicated elements, leaving only the unique element with constant space usage and linear time complexity.

How does XOR help in finding the unique element?

XOR works by cancelling out pairs of equal numbers, as x ^ x = 0, leaving the unique element as the result after processing the entire array.

Can we use a hash map to solve the Single Number problem?

Using a hash map would require extra space, which violates the problem's requirement for constant space. XOR provides a more efficient solution with O(1) space complexity.

What is the time complexity of the XOR approach for this problem?

The time complexity of the XOR approach is O(n), where n is the number of elements in the array. Each element is processed once.

Can negative numbers be part of the input array?

Yes, the XOR approach works regardless of whether the numbers are positive or negative, as XOR is based on binary representation.

GhostInterview Solver

Need direct help with Single Number instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Single Number from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.