Two Sum
L3 Easy Arrays & Hashing
A Dictionary maps keys to values with ~O(1) lookup. As you walk the array, you can ask the dictionary whether the number you still need has already been seen.
You are given an array of integers nums and a target. Two distinct numbers in nums add up to target. Return the indices of those two numbers, in any order. You may assume exactly one valid pair exists, and you may not use the same index twice.
Examples
▸ nums = [2, 7, 11, 15], target = 9
→ [0, 1]
▸ nums = [3, 2, 4], target = 6
→ [1, 2]
▸ nums = [3, 3], target = 6
→ [0, 1]