Two Sum

Given an array and a target, find two numbers that add up to it. The one-pass hash map turns a nested search into a single question per element: have I seen your complement?

The memory hook

Check the complement before you insert — each element asks one question of the map, then becomes an answer for the future.

EasyHash mapO(n) timeO(n) space
01

Ask for the complement

intuition

Brute force re-scans the array for every element. Flip the search around: as you walk left to right, keep every value you have already seen in a map. For the current x, the partner you need is forced — need = target − x — so the whole problem becomes one map lookup per element.

1Compute the needThe partner is forced: need = target − x. No choice, no search.
2Ask the mapIf need was seen before, its stored index plus i is the answer — return immediately.
3Store, then move onOtherwise record x → i. Checking first guarantees the two indices are distinct.
The one sentence to remember: look up target − x before storing x, so every pair is caught the moment its second half arrives.
02

Watch the map remember

playable trace

Step through the scan. Each frame reads one element, asks the map a single question, and either returns the pair or files the element away for later. Type your own array as nums; target.

Playable trace · Hash map
Frames are generated from the same logic as the code below.
Presets
Current event
Ready — press Play
Answer pair
—
Seen map · value → index
The map starts empty. Press Play and watch each element ask one question before being stored.
unread current in map complement missed answer pair
0 / 0
03

The one-pass scan

the solution
Python · Hash map
def twoSum(nums, target):
    seen = {}  # value -> index
    for i, x in enumerate(nums):
        need = target - x
        if need in seen:  # partner arrived earlier
            return [seen[need], i]
        seen[x] = i  # file away for the future
    raise ValueError("no solution")

Check-then-store is the whole trick: the lookup happens before the insert, so an element can never pair with itself.

Why distinct indiceslookup before insertneed is queried before x is stored, so seen[need] is always an earlier index — never i itself, even for [3, 3].
Why first answer winsreturn on first hitThe problem guarantees exactly one solution, so the first complement found is the answer; no tie-breaking needed.
Duplicatesseen[x] = i overwritesOverwriting is safe: any pair using the older index would already have returned when its complement arrived.
Why can't the answer hide? Every pair has a second arrival. When the later half is read, the earlier half is already in the map — the lookup cannot miss it.
TimeO(n)n elements, one O(1) map lookup and one insert each.
SpaceO(n)The map holds at most n value → index entries.
04

Two quick mental checks

make it stick
nums = [2,7,11,15], target = 9
[0, 1]

2 is stored; 7 needs 2, which the map already holds — return [0, 1] without reading further.

nums = [3,3], target = 6
[0, 1]

The first 3 is stored before the second arrives, so the complement 3 is found — the edge case that breaks insert-first variants.

What to say out loud
I'll walk the array once, keeping a map of values I've seen. For each number I compute the partner it needs, check the map, and only then store it — so the first hit I find is the answer, in linear time.