←
Atlas · Linear foundations · Topic 1 of 18

Arrays & Hashing

Remember what you've seen in a hash map or set, so every look-up is instant instead of a second loop.

Watch in Lens

Spot it

Signals in the question

You need to know quickly whether something has appeared before (duplicates, pairs, counts).

A brute force compares every pair: O(n²) that a lookup could make O(n).

The answer depends on counts or groups (anagrams, frequencies, top-k).

The idea

O(n) time · O(n) space

Walk the list once. For each item, ask the hash map a question ("have I seen its partner?") and then store the item for later ones. Each question costs O(1), so one pass is O(n).

Template · Seen-before map

1def two_sum(nums, target):2    seen = {}3    for i, x in enumerate(nums):4        if target - x in seen:5            return [seen[target - x], i]6        seen[x] = i7    return []

The template, filled in for Two Sum. The highlighted lines are the part every problem of this kind shares.

The best, free

One of each
WatchArrays & Hashing, explainedNeetCode · YouTube · freeReadHashing in data structuresGeeksforGeeks · free

Practice set

Loading your mission…

Common mistakes

1Storing the item before checking, so it matches itself (x + x = target).

2Using a list for "seen": in on a list is O(n), so the whole thing becomes O(n²).

3Forgetting what to return when there's no answer.

Check yourself

Question 1 of 2

"Does any value appear at least twice?" What's the fastest simple approach?