Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 1 addition & 1 deletion data/book-sets.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"tags": [],
"problems": [
1, 2, 11, 12, 13, 14, 15, 17, 19, 20, 21, 22, 23, 25, 26, 27, 28, 30, 31, 33, 34, 35, 36, 39, 42, 45, 46, 48, 49, 50, 52, 53, 54, 55, 56, 57,
58, 61, 63, 64, 66, 67, 68, 69, 70, 71, 73, 76, 77, 79, 80, 82, 86, 88, 92, 97, 98, 125, 138, 139, 141, 146, 148, 149, 150, 151, 153, 155, 162,
58, 61, 63, 64, 66, 67, 68, 69, 70, 71, 73, 76, 77, 79, 80, 82, 86, 88, 92, 97, 98, 125, 136, 138, 139, 141, 146, 148, 149, 150, 151, 153, 155, 162,
167, 169, 172, 173, 188, 189, 190, 191, 198, 199, 200, 201, 202, 205, 207, 208, 209, 210, 211, 212, 215, 217, 221, 222, 224, 226, 228, 230, 238,
242, 268, 274, 283, 289, 290, 295, 300, 334, 344, 345, 373, 380, 383, 392, 399, 427, 433, 443, 452, 502, 530, 605, 637, 643, 647, 739, 901, 909,
918, 1004, 1071, 1207, 1431, 1456, 1657, 1679, 1732, 1768, 1798, 1957, 1991, 2215, 2352, 2862, 3424, 3602, 3603, 3604, 3605, 3606, 3607, 3608,
Expand Down
98 changes: 98 additions & 0 deletions explanations/136/en.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# 136. Single Number [Easy]

[https://leetcode.com/problems/single-number](https://leetcode.com/problems/single-number)

## 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:**
```text
Input: nums = [2,2,1]
Output: 1
```

**Example 2:**
```text
Input: nums = [4,1,2,1,2]
Output: 4
```

**Example 3:**
```text
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:

```text
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 you XOR all numbers, pairs of identical numbers will cancel out to 0, leaving only the single number.

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

for num in nums:
result ^= num # XOR

return result
Comment on lines +1 to +7
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Missing import for List type hint.

The algorithm correctly uses XOR properties to find the single number, but the List[int] type hint requires an import.

Add the missing import at the top of the file:

+from typing import List
+
 def singleNumber(nums: List[int]) -> int:
     result = 0
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def singleNumber(nums: List[int]) -> int:
result = 0
for num in nums:
result ^= num # XOR
return result
from typing import List
def singleNumber(nums: List[int]) -> int:
result = 0
for num in nums:
result ^= num # XOR
return result
🧰 Tools
🪛 Ruff (0.12.2)

1-1: Undefined name List

(F821)

🤖 Prompt for AI Agents
In solutions/136/01.py at lines 1 to 7, the List type hint is used but not
imported. Add the missing import statement "from typing import List" at the top
of the file to properly support the List[int] annotation.