#1
Two Sum
#1
Pattern: Hash Map for Complement Lookup
1. Create an empty hash map to store {value: index}
2. Iterate through the array once:
- Calculate complement = target - nums[i]
- If complement exists in map, return [map[complement], i]
- Otherwise, store nums[i] with its index
3. Key insight: We only need one pass because we check before adding
Edge cases:
- Same element used twice (handle by checking before adding)
- Negative numbers (works fine with hash map)
Time: O(n) - single pass
Space: O(n) - hash map storage
Click the card to see demo notes