Arrays & Hashing

Learn arrays and hashing through animated coding interview problems, including Two Sum and anagrams. Compare approaches and explore Python solutions.

The pattern

Arrays & Hashing: trade a little memory for a lot of speed

Most “find”, “count” and “have I seen this?” questions have a slow answer that compares everything with everything (O(n²)) and a fast answer that walks the data once while remembering what it saw in a hash set or hash map (O(n)). This category is where every other pattern starts, which is why NeetCode puts it first.

list: check each one38194726hash set: jump to it4?12346

1Scan vs look up

Searching a list checks items one by one: up to n steps. A hash set or map jumps straight to where the value would be: about one step.

“listen”eilnst“silent”eilnstsame counts → anagrams

2Count, don't compare

“listen” and “silent” look different, but their letter counts are identical. Counting with a map turns “same letters?” into a quick check.

prefix: product so far →1234← suffix: product so faranswer[i] = prefix × suffix

3Precompute in a pass

Sweep once left to right and once right to left, carrying a running product or sum. Then each answer is ready without re-scanning.

How to spot it

  • “Have I seen this before?”, “any duplicates?” → a hash set.
  • “Find a partner / complement” (like target − x) → a hash map of what you've seen.
  • “Same letters”, “how many of each”, “most frequent” → count with a hash map.
  • “Group things that are equivalent” → find a key every member shares, then hash on it.
  • “Everything except i”, range sums or products → prefix and suffix passes.

The toolkit (Python)

  • seen = set(): add / check membership in O(1) on average
  • index_of = {}: value → position, or key → anything
  • Counter(xs) or a dict: value → how many
  • defaultdict(list): key → group of items
  • prefix: a running total carried through one pass

Hash lookups are O(1) on average; a badly distributed hash can make them slower, which is why answers say “on average”.

NeetCode 150 · Arrays & Hashing

All 9 problems

Pick a problem to see it solved step by step. They're in NeetCode's order, roughly easiest first.

Contains Duplicate

EasyBlind 75

Given a list of numbers, return true if any number appears more than once, and false if every number is different.

Input

nums = [1, 2, 3, 1]

nums

1
i
2
3
1

seen (hash set)

(empty)

variables

i
0

Press Play or Step to watch the solution run.

Step 0 / 7
900 ms

Brute force vs the pattern

On this input:

Pattern: O(n)
7 ops
Brute force
3 ops

On a bigger input (200 different numbers):

Pattern
400 ops
Brute force
19,900 ops

Compare every pair of numbers: about n²/2 comparisons.

“ops” counts basic steps: lookups, inserts, comparisons, array reads. On tiny inputs brute force can even win, because it may stumble on the answer early; as n grows, the gap opens up.

How it works: Contains Duplicate

Step by step

  • Number being checked
  • The duplicate
  • Checked, all different

Press Start or Step: the step the animation is on lights up below.

  1. 1

    Look it up

    Check whether the current number is already in the set.

    if n in seen
  2. 2

    Remember it

    Not there yet, so add it and move on.

    seen.add(n)
  3. 3

    Already there → duplicate

    A lookup hit means we saw this number earlier.

    return True
  4. 4

    End of list → all different

    No lookup ever hit, so no value repeats.

    return False

Checking every pair works, but it's slow: 10,000 numbers means about 50 million comparisons.

A hash set answers “have I seen this before?” in about one step. Walk through the list once: if the number is already in the set, it's a duplicate; if not, add it.

Spot it in an interview

  • “Has this appeared before?”, “any repeats?”, “first repeated value”: reach for a hash set.
  • Trading memory (the set) for speed is the most common move in this whole category.
time O(n)space O(n)

One pass, and each set lookup or insert takes constant time on average. The set can hold up to n numbers.

Credits

Learn more from NeetCode

The problem list and order come from the NeetCode 150 by NeetCode (neetcode.io). The solutions here follow the approaches NeetCode teaches in the linked videos; the animations, explanations and code were written for CodeQuest. If these help, NeetCode's videos and site are well worth your time.

Problem statements are summarised in our own words; the full versions are on LeetCode.