136. Single Number - Detailed Explanation

Problem statement

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

Note:
Your algorithm should have a linear runtime complexity and use constant extra space.

Example inputs and outputs

  1. Example 1:

    • Input: [2, 2, 1]
    • Output: 1
    • Explanation:
      The number 1 appears only once, whereas 2 appears twice.
  2. Example 2:

    • Input: [4, 1, 2, 1, 2]
    • Output: 4
    • Explanation:
      The number 4 is the only element that appears once while the others appear twice.

Constraints

  • 1 <= nums.length <= 3 * 10^4
  • -3 * 10^4 <= nums[i] <= 3 * 10^4
  • Every element in the array appears exactly twice except for one.

This question is the standard introduction to the bitwise XOR pattern. XOR, written ^, compares two numbers bit by bit and sets each output bit to 1 only where the two input bits differ. Two consequences of that rule do all the work here: a ^ a = 0, and 0 ^ a = a. XOR is also commutative and associative, so the order of the array does not matter and each pair cancels wherever its two copies happen to sit. That property converts a counting problem into a single accumulator variable, and it reappears in several questions on the list of patterns interviewers reuse. This question also sits on the Blind 75 list. Design Gurus teaches it in the Single Number lesson.

Hints for solving the problem

  1. Hash table approach:

    • You can count the frequency of each element using a hash table, then identify the one with a count of 1.
  2. XOR approach:

    • Consider using the bitwise XOR operator.
    • Properties of XOR:
      • a ^ a = 0
      • 0 ^ a = a
    • When you XOR all elements together, the pairs cancel each other, which leaves only the single element.

Approach 1: brute force using a hash table

Explanation

  • Traverse the array and use a dictionary, or hash map, to count the occurrences of each element.

  • Iterate through the hash map to find the element with a count of 1.

  • Time complexity: O(n)

  • Space complexity: O(n) in the worst case.

Python code (brute force)

Python3
Python3

. . . .

Java code (brute force)

Java
Java

. . . .

Approach 2: optimal solution using XOR

Explanation

  • Initialize a variable result with 0. Zero is the identity value for XOR, so it does not disturb the running total.

  • Iterate through each number in the array and perform the XOR operation with result:

    • result ^= num
  • The XOR operation cancels every duplicate number, since a ^ a = 0, and leaves the unique number behind.

  • Time complexity: O(n)

  • Space complexity: O(1)

Trace [4, 1, 2, 1, 2] to see the cancellation. The running result goes 0, 4, 5, 7, 6, and finally 4. The intermediate values look arbitrary, and they do not matter. What matters is that each of 1 and 2 enters the total twice, so both vanish, and only 4 survives.

Python code (optimal XOR)

Python3
Python3

. . . .

Java code (optimal XOR)

Java
Java

. . . .

Complexity analysis

  • Brute force (hash table):
    • Time complexity: O(n)
    • Space complexity: O(n)
  • Optimal (XOR):
    • Time complexity: O(n)
    • Space complexity: O(1)

Both approaches read the array once, so the time complexity matches. The XOR version wins on space, because it replaces the whole frequency map with one integer. That is what satisfies the constant extra space requirement in the note above.

Edge cases

  1. A single-element array:
    • For example, [x] should return x. One XOR against the initial 0 returns the element unchanged.
  2. An array with negative numbers:
    • The XOR approach works correctly with negative numbers as well, because it operates on the two's complement bit patterns.
  3. Large arrays:
    • Confirm that your solution handles arrays of the maximum allowed size efficiently.

Common mistakes

  1. Using extra space unnecessarily:
    • The hash table approach is correct, but it does not meet the optimal space requirement.
  2. Incorrect XOR initialization:
    • Forgetting to initialize the result to 0, or misusing the XOR operations, leads to a wrong answer.
  3. Overlooking edge cases:
    • Check that your solution handles a single-element array and arrays with negative numbers.

Alternative variations

  1. Single Number II (LeetCode 137):
    • Every element appears three times except for one. A pair of XORs no longer cancels a triple, so this variant needs a different bit manipulation approach.
  2. Single Number III (LeetCode 260):
    • Two numbers appear once while all others appear twice. The task is to find both unique numbers, which means splitting the array by one differing bit.
  1. Single Number II (LeetCode 137)
  2. Single Number III (LeetCode 260)
  3. Missing Number (LeetCode 268)
  4. Find the Duplicate Number (LeetCode 287)

All three Single Number variants, plus the rest of the bitwise XOR set, are taught in Grokking the Coding Interview. For a short map of the families these questions fall into, see the common LeetCode patterns answer.

TAGS
Coding Interview Questions
Coding Interview
CONTRIBUTOR
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
What does Microsoft ask in interview?
What happens in a 15 minute interview?
Which is the world's toughest interview?
Why do I want to join answer?
What skills are required for cloud computing?
What Is the Dream11 Interview Process Like? (Round by Round)
Candidates report five Dream11 rounds: a coding round, a database and project round, a high level design round, a hiring manager round, and an HR round.
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.6
(3,192 learners)
Discounted price for Your Region

$99

Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
4.1
Discounted price for Your Region

$72

Design Gurus logo
One-Stop Portal For Tech Interviews.
About Us
Our Team
Careers
Partners
Contact Us
Become Affiliate
Copyright © 2026 Design Gurus, LLC. All rights reserved.