Watch 10 video solutions for Single Number, a easy level problem involving Array, Bit Manipulation. This walkthrough by NeetCode has 143,067 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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 * 104Problem 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 | Time | Space | When to Use |
|---|---|---|---|
| Hash Map Counting | O(n) | O(n) | When simplicity and clarity matter more than memory usage |
| XOR Bit Manipulation | O(n) | O(1) | Best general solution when duplicates appear exactly twice |