Single Number
Every element appears twice except one. Find the single one. Use O(1) extra space Example: [2,2,1] => 1
XOR cancels equal pairs and keeps the odd one out. One pass over the array, no extra space.
Examples
You write
single_number(nums: List[int])
Example 1
- Input
- [2,2,1]
- returns
- 1
Example 2
- Input
- [4,1,2,1,2]
- returns
- 4
The pattern behind it
Arrays & Hashing
I reach for it when
- You catch yourself scanning the array again for every element.
- The problem asks about duplicates, pairs, counts or groups.
Say this before you type. Say what the key is and what the value remembers, before you write the loop.
All 127 Arrays & Hashing problemsBit Manipulation
I reach for it when
- The statement bans extra space or restricts operators.
- Numbers act as flags, masks or XOR pairs.
Say this before you type. Say what each bit means in this problem before you shift or mask anything.
All 12 Bit Manipulation problems
Meet Coco
Coco is an AI study partner, not a person. She listens while you explain your code, asks what a line does and never hands you the fix. Sign in free to type with Coco, up to a daily limit. The editor and Run stay open without an account. Anyone can open the editor and run their code.