Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions explanations/136/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
# 136. Single Number

**Difficulty:** Easy
**Link:** https://leetcode.com/problems/single-number/

## Problem Description

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 * 10^4`
- `-3 * 10^4 <= nums[i] <= 3 * 10^4`
- Each element in the array appears twice except for one element which appears only once.

## Explanation

### Strategy

This is a **bit manipulation problem** that requires finding the single element in an array where all other elements appear twice. The key insight is to use the XOR (exclusive or) operation, which has special properties that make it perfect for this problem.

**Key observations:**
- XOR of a number with itself is 0: `a ^ a = 0`
- XOR of a number with 0 is the number itself: `a ^ 0 = a`
- XOR is associative and commutative: `a ^ b ^ a = (a ^ a) ^ b = 0 ^ b = b`
- All elements appear twice except one, so XORing all elements will cancel out the pairs

**High-level approach:**
1. **Initialize result**: Start with `result = 0`
2. **XOR all elements**: Iterate through the array and XOR each element with the result
3. **Return result**: The final result will be the single number

### Steps

Let's break down the solution step by step:

**Step 1: Initialize result**
- Start with `result = 0`

**Step 2: XOR all elements**
For each number in the array:
- XOR the current number with the result: `result = result ^ num`
- This will cancel out pairs and leave the single number

**Step 3: Return the result**
- The final result is the single number

**Example walkthrough:**
Let's trace through the second example:

```
nums = [4,1,2,1,2]

Initial state:
result = 0

Step 1: result = 0 ^ 4 = 4

Step 2: result = 4 ^ 1 = 5

Step 3: result = 5 ^ 2 = 7

Step 4: result = 7 ^ 1 = 6

Step 5: result = 6 ^ 2 = 4

Result: Return 4
```

**Why this works:**
- `4 ^ 1 ^ 2 ^ 1 ^ 2`
- `= 4 ^ (1 ^ 1) ^ (2 ^ 2)`
- `= 4 ^ 0 ^ 0`
- `= 4 ^ 0`
- `= 4`

> **Note:** The XOR operation is perfect for this problem because it has the property that `a ^ a = 0` and `a ^ 0 = a`. This means that when we XOR all numbers, pairs of identical numbers will cancel out to 0, leaving only the single number.

### Solution

```python
class Solution:
def singleNumber(self, nums: List[int]) -> int:
# Initialize result
result = 0

# XOR all numbers in the array
for num in nums:
result ^= num

# Return the single number
return result
```

**Time Complexity:** O(n) - we visit each element exactly once
**Space Complexity:** O(1) - we only use a constant amount of extra space
11 changes: 11 additions & 0 deletions solutions/136/01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def singleNumber(self, nums: List[int]) -> int:
# Initialize result
result = 0

# XOR all numbers in the array
for num in nums:
result ^= num

# Return the single number
return result