LeetCode 1 · Easy

Two Sum Explained: Hash Map in One Pass (LeetCode 1)

Updated Nov 27, 2026 · 3 min read

Walk the list once with a hash map from value to index. For each number x, check whether target minus x is already in the map; if it is, return its index and the current one. If not, store x and move on. That's O(n) time and O(n) space, instead of O(n²) for checking every pair.

Watch onYouTube

Key points

  • ●Brute force checks every pair with two loops: O(n²) time, O(1) space.
  • ●For each x there's only one partner that works: target - x.
  • ●Look up first, then store, so an element never pairs with itself.
  • ●The hash map trades O(n) memory for O(n) time.
Step-by-step visual sheet: Two Sum
The full visual sheet, made with Edithly.Open the interactive sheet

The problem: find the pair

You get a list of integers and a target. Two of the numbers add up to the target, and exactly one pair works. Return their positions. You can't use the same element twice.

Take nums = [2, 7, 11, 15] and target = 9. The answer is [0, 1], because 2 + 7 = 9.

Notice what's being asked for: positions, not the two values. That detail shapes the whole solution.

The slow way: check every pair

The first idea is two loops. Pick a first index i, then try every later index j, and check whether the two numbers add to the target.

for i in range(len(nums)):
    for j in range(i + 1, len(nums)):
        if nums[i] + nums[j] == target:
            return [i, j]

Starting j at i + 1 stops an element from pairing with itself and skips pairs you've already tried. It always works, but it checks up to n(n-1)/2 pairs. That's O(n²) time.

The idea: ask for the missing number

Rearrange the sum. If the current value is x and the partner is c, then x + c = target, so c = target - x. For any x, there's only one number that could finish the pair.

So instead of searching for a partner, you ask a yes-or-no question: "have I already seen target - x?" A hash map answers that in average O(1) time. Store each value you pass along with its index, and look up the complement for every new number.

Step 3 of the visual sheet above draws this rearrangement, and step 4 shows the order that makes it safe: look up first, then store.

Trace it on the example

i value complement (9 - value) seen before lookup result
0 2 7 {} not found, store 2 → 0
1 7 2 {2: 0} found, return [0, 1]

At index 1 the number 7 needs a 2, and 2 was stored at index 0. You stop right there and never look at 11 or 15.

The code in Python

def two_sum(nums, target):
    seen = {}
    for i, value in enumerate(nums):
        complement = target - value
        if complement in seen:
            return [seen[complement], i]
        seen[value] = i
    return []

Run it on [2, 7, 11, 15] with target 9 and it returns [0, 1]. The final return [] only matters if there's no pair, which the problem promises won't happen.

Complexity

Approach Time Space
Brute force O(n²) O(1)
One-pass hash map O(n) O(n)

You spend extra memory on the map so you never have to search the earlier numbers again. Hash map lookups are O(1) on average, and each element is visited once.

The trap to watch for

Storing before you look up. Say the target is 6 and the list contains a single 3. If you add 3 to the map first, the lookup for 6 - 3 finds that same 3 and you return one index twice. Checking first means the map only ever holds earlier elements, so the pair is always two different positions.

Duplicates are fine, by the way. With [3, 3] and target 6, the first 3 is stored at index 0, the second 3 finds it, and you get [0, 1].

This "store what you've seen, look up what you need" move is the hash map pattern, and you'll see it again in problems like Contains Duplicate and in the setup for 3Sum.

Frequently asked questions

What is the time complexity of Two Sum?

The one-pass hash map solution is O(n) time on average, because each lookup and insert is average O(1), and O(n) space for the map. The two-loop brute force is O(n²) time and O(1) space.

Why check the map before adding the current number?

So the current number can't match itself. With target 6 and the number 3, adding 3 first would make it find its own 3 and return the same index twice, which the problem doesn't allow.

Can you solve Two Sum by sorting and using two pointers?

Sorting works for finding the values, but the problem asks for the original positions, so you'd have to carry the indices through the sort. The hash map keeps them for free in one pass.

What does the hash map store in Two Sum?

Each value you've already passed, mapped to its index. When a later number needs that value, you read its index straight from the map.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free