Skip to main content

Single Number - Solution & Explanation

EasyArrayBit Manipulation15 min readAsked at: Amazon, Microsoft, Meta +12
Practice this problem

Problem Statement

Given a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

 

Example 1:

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

Example 2:

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

Example 3:

Input: nums = [1]
Output: 1

 

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.

Approach Overview

Problem Overview: You receive an integer array where every element appears exactly twice except for one element that appears only once. The task is to find that single element. The solution must work efficiently even when the array grows large.

Approach 1: Hash Map Counting (O(n) time, O(n) space)

This approach uses a frequency map to count how many times each number appears. Iterate through the array and store counts using a hash map where the key is the number and the value is its frequency. After building the map, scan through the entries and return the number with frequency 1. The logic is straightforward and easy to implement, which makes it a good baseline approach when you first reason about the problem. However, it requires extra memory proportional to the number of unique elements.

This technique relies on constant-time hash lookups and works well for many array counting problems. The tradeoff is memory usage since the hash map stores up to n keys.

Approach 2: XOR Operation (O(n) time, O(1) space)

The optimal solution uses properties of the XOR bitwise operator. XOR has two useful rules: a ^ a = 0 and a ^ 0 = a. When you XOR all numbers in the array together, duplicate numbers cancel each other out because each pair produces zero. The only value left after the full pass is the number that appears once.

Implementation is simple: initialize a variable result = 0, iterate through the array, and update result ^= num for each element. After processing the entire array, result holds the unique element. The algorithm performs a single pass and uses only one variable for storage, giving constant extra space.

This approach is a classic example of using bit manipulation to reduce memory usage. It avoids auxiliary data structures while maintaining linear runtime, which makes it both elegant and efficient.

Recommended for interviews: Interviewers usually expect the XOR solution. Starting with the hash map approach demonstrates you understand the problem using counting and hash lookups. Moving to the XOR technique shows deeper algorithmic thinking and familiarity with bitwise operations. The XOR solution achieves the optimal O(n) time and O(1) space, which is the key improvement interviewers look for.

Approach 1: Approach 1: XOR Operation

This approach leverages the properties of XOR bitwise operation. The XOR of a number with itself is 0, and the XOR of a number with 0 is the number itself. Thus, XORing all elements in the array results in getting the single number because all other numbers cancel themselves out.

The function singleNumber iterates over the array and continuously performs an XOR operation. By the end of the loop, all pairs will cancel out, leaving the single number as the result.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - We go through the array once.
Space Complexity: O(1) - We only use a single integer to store the result.

Try this approach in the editor →

Approach 2: Approach 2: Hash Map

In this approach, we use a hash map (or dictionary) to count occurrences of each number. The single number will have a count of 1. This isn't the optimal solution in terms of extra space but is valid if space was not constrained.

This C solution uses an array to simulate a hash map, where each index corresponds to a potential value of nums[i]. It maps numbers from -30000 to +30000 to indices 0 to 60000 to handle negative numbers.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n) - We traverse the array twice (once for filling the map, once for checking the counts).
Space Complexity: O(n) - Extra space proportional to the input range.

Try this approach in the editor →

Approach 3: Bitwise Operation

The XOR operation has the following properties:

  • Any number XOR 0 is still the original number, i.e., x \oplus 0 = x;
  • Any number XOR itself is 0, i.e., x \oplus x = 0;

Performing XOR operation on all elements in the array will result in the number that only appears once.

The time complexity is O(n), where n is the length of the array. The space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Rust

JavaScript

C#

C

Swift

Try this approach in the editor →

Approach 4: Default Approach

Code

Java

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Approach 1: XOR Operation

Time Complexity: O(n) - We go through the array once.
Space Complexity: O(1) - We only use a single integer to store the result.

Approach 2: Hash Map

Time Complexity: O(n) - We traverse the array twice (once for filling the map, once for checking the counts).
Space Complexity: O(n) - Extra space proportional to the input range.

Bitwise Operation—
Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Hash Map CountingO(n)O(n)When simplicity and clarity matter more than memory usage
XOR Bit ManipulationO(n)O(1)Best general solution when duplicates appear exactly twice

Video Solution

Single Number - Leetcode 136 - Python • NeetCode • 148,005 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Single Number easy or hard?
Single Number is categorized as an Easy problem on LeetCode. The hash map solution is simple to implement, while the XOR solution introduces a useful bit manipulation trick that appears in many technical interviews.
Single Number Python/Java solution
In Python or Java, the typical solution iterates through the array and XORs each element with a running variable. For example, start with result = 0 and update result ^= num for every value. After the loop finishes, result contains the single non-duplicated number.
How to solve Single Number in O(n)?
Iterate through the array and apply XOR with an accumulator variable. Because XOR cancels duplicate numbers, every pair disappears during the process. After processing all elements, the accumulator holds the element that appears once. The algorithm requires one pass and constant memory.
What is the best approach for Single Number?
The XOR bit manipulation approach is the best solution. By XORing all numbers in the array, duplicate values cancel each other out because a ^ a = 0. The remaining value is the unique number. This method runs in O(n) time and uses O(1) extra space.
Is Single Number asked at Google/Amazon/Meta?
Single Number is a common interview-style question used by companies like Amazon, Google, and Meta to evaluate understanding of bit manipulation and algorithmic reasoning. Variations of the problem often appear in coding screens and technical interviews.
What data structure is used in Single Number?
Two common techniques are used: a hash map for frequency counting and bit manipulation using XOR. The hash map approach stores counts for each number, while the XOR approach avoids extra data structures and relies on bitwise properties.
What is the time complexity of Single Number?
The optimal solution runs in O(n) time because the algorithm scans the array once and performs a constant-time XOR operation for each element. Space complexity is O(1) since only a single variable is used to track the result.

Ready to solve this problem?

Practice Single Number with our built-in code editor and test cases.

Practice on FleetCode