beginner7 min read·Updated September 17, 2026
Two Sum with a Hash Map Explained: Tracing [3, 2, 4] and [3, 3]
Master the Two Sum hash map pattern with a complete walkthrough of [3, 2, 4] and [3, 3]. Learn the mental model, complement logic, and O(n) time complexity.
By Learnisim AI·Published September 17, 2026
LEARNING ARCSetup → Model in the example → Full trace → Twist → Edge cases → Apply
PREREQUISITES
- Basic arrays and indexing
- Basic hash map or dictionary operations
Why
Why Nested Loops Fail Us on [3, 2, 4]
Imagine you are handed the array and a target sum of 6. You need to find two distinct indices whose values add up to 6. If you try the most obvious approach—checking every possible pair with nested loops—you look at , then , and finally . On a tiny array, that feels trivial. But what happens when your array grows to 100,000 numbers? Your computer starts burning CPU cycles recalculating sums it has already seen, scaling quadratically at time complexity. Worse yet, consider what happens on with target 6: a naive loop might accidentally pair index 0 with itself, returning , which violates the rule that you must use distinct indices. We need a way to look back in constant time at what numbers we have already passed, without rescanning the entire array from scratch.
Model
The Single-Pass Hash Map Model on [3, 2, 4] Target 6
Building on our motivation from the brute-force failure, how can we solve
nums = [3, 2, 4] with target = 6 in a single left-to-right pass? Instead of asking a second loop to scan the rest of the array, we ask a hash map to remember what we have already seen behind us. As we visit each number at index , we immediately calculate its complement: . If that complement is already sitting inside our hash map, we have our two indices instantly. If it is not there yet, we store alongside its index and move one step forward.Syntax
Syntax and APIs for Two Sum on [3, 2, 4]
To implement our single-pass model on
nums = [3, 2, 4] and target = 6, we need a concrete data structure. In Python, a dictionary acts as our hash map, mapping each number to its index. As we loop through the array with enumerate(nums), we check if the complement target - num already exists in our dictionary before we store the current number. If it exists, we immediately return the stored index and our current index. If it does not, we record num: i in the dictionary and move forward.Worked example
Walking Through Two Sum on [3, 2, 4] Target 6 Step-by-Step
Let's trace our algorithm completely on the locked example:
nums = [3, 2, 4] with target = 6. We maintain an empty hash map seen to store { value: index } as we move from left to right.Given:
nums = [3, 2, 4], target = 6Steps:
1. Index 0: Element is
2. Index 1: Element is
3. Index 2: Element is
1. Index 0: Element is
3. Complement is . We check if 3 is in our seen map. It is not (map is empty). We store our current value and index: seen[3] = 0. Map state: {3: 0}.2. Index 1: Element is
2. Complement is . We check if 4 is in seen. It is not (seen only has key 3). We store: seen[2] = 1. Map state: {3: 0, 2: 1}.3. Index 2: Element is
4. Complement is . We check if 2 is in seen. Match found! Key 2 exists at index 1. We immediately return [seen[2], 2], which evaluates to [1, 2].Result: Returns
[1, 2]. This correctly references values nums[1] = 2 and nums[2] = 4, which sum to .Practice
Practice: Predict the Hash Map State and Return Indices for [3, 3] Target 6
Now it is your turn to apply the single-pass hash map algorithm we traced for
[3, 2, 4] to our second working example: nums = [3, 3], target = 6. Recall that our core rule was checking the complement before inserting the current element to prevent pairing an element with itself. Work through the iterations at index 0 and index 1, tracking what the hash map contains at each step.Apply
Applying the Complement Pattern to Find Three Sum Indices
Now that you have mastered the single-pass hash map pattern on our working examples
[3, 2, 4] target 6 and [3, 3] target 6, it is time to transfer this exact mental model to a neighboring problem: finding pairs in a stream, or adapting the logic when requirements shift. Consider how the core principle—checking for target - x in time before inserting x into memory—prevents self-pairing and eliminates redundant nested loops. When you encounter variations like finding if any two numbers sum to a target in a sorted versus unsorted array, or adapting the hash map to store frequencies instead of indices, the foundational insight remains identical. You are always trading space for time by caching past states so future lookups happen instantly.FAQ
How does the hash map approach solve Two Sum in O(n) time?
Instead of using nested loops to check every possible pair (which takes O(n²) time), we store each number and its index in a hash map as we iterate. For every number, we instantly check if its complement (target - current number) already exists in the map.
Why does the trace on [3, 3] with target 6 return [0, 1] instead of [0, 0]?
By checking for the complement before inserting the current number into the hash map, we prevent an element from being paired with itself. When the second '3' is evaluated, the first '3' is already in the map, correctly yielding indices [0, 1].
What happens if there are multiple valid solutions for Two Sum?
Most standard Two Sum problems guarantee that exactly one solution exists, and you can return the indices in any order. The single-pass hash map approach will naturally return the first valid pair it completes.
What is the space complexity of the Two Sum hash map solution?
The space complexity is O(n) in the worst case, because we store up to n elements and their indices in the hash map if the valid pair is found at the very end of the array.