136. Single Number - Detailed Explanation

Free Coding Questions Catalog
Boost your coding skills with our essential coding questions catalog. Take a step towards a better tech career now!

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.

Hints for Solving the Problem:

  1. Hash Table Approach:

    • You can count the frequency of each element using a hash table and 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, pairs cancel each other out, leaving 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.

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

    • result ^= num
  • The XOR operation cancels out duplicate numbers (because a ^ a = 0), leaving the unique number.

  • Time Complexity: O(n)

  • Space Complexity: O(1)

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)

Edge Cases:

  1. Single Element Array:
    • Example: [x] should return x.
  2. Array with Negative Numbers:
    • The XOR approach works correctly with negative numbers as well.
  3. Large Arrays:
    • Ensure the algorithm can handle arrays of maximum allowed size efficiently.

Common Mistakes:

  1. Using Extra Space Unnecessarily:
    • While the hash table approach is valid, it does not meet the optimal space requirement.
  2. Incorrect XOR Initialization:
    • Forgetting to initialize the result to 0 or misusing XOR operations can lead to errors.
  3. Overlooking Edge Cases:
    • Ensure that the solution handles cases such as a single-element array or arrays with negative numbers.

Alternative Variations:

  1. Single Number II (LeetCode 137):
    • Every element appears three times except for one. A different bit manipulation approach is needed.
  2. Single Number III (LeetCode 260):
    • Two numbers appear once while all others appear twice. The problem requires finding the two unique numbers.

Related Problems for Further Practice:

  1. Single Number II (LeetCode 137)
  2. Single Number III (LeetCode 260)
  3. Missing Number (LeetCode 268)
  4. Find the Duplicate Number (LeetCode 287)
TAGS
leetcode
CONTRIBUTOR
Design Gurus Team
-

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
Which strategy does Netflix use?
How to lead a technical interview?
How to understand distributed computing concepts for interviews?
Related Courses
Image
Grokking the Coding Interview: Patterns for Coding Questions
Grokking the Coding Interview Patterns in Java, Python, JS, C++, C#, and Go. The most comprehensive course with 476 Lessons.
Image
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
Image
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
Image
One-Stop Portal For Tech Interviews.
Copyright © 2025 Design Gurus, LLC. All rights reserved.
;