leetcode-textbook

Pattern 1 - Arrays & Hashing

This is the first and most-used pattern in the book. Master it before moving on. Almost every interview set opens with one of these problems because the techniques here (hash maps and hash sets) underpin solutions in every later pattern.


What the pattern is

Trade memory for time. A brute-force check “does X already exist?” or “how many times has X appeared?” costs O(n) per question if you scan the array. A hash structure answers the same question in O(1) average time, at the cost of O(n) extra space.

Concretely, the pattern gives you two tools:

One pass through the input, one O(1) lookup per element, one O(1) insert. Total: O(n) time.


When it applies – trigger signals

Reach for this pattern when the problem statement contains any of these words:

Trigger signal Example problem Which tool
“duplicate”, “appears twice”, “any value … more than once” Contains Duplicate (217) HashSet
“two numbers that sum to”, “pair”, “complement” Two Sum (1) HashMap (value -> index)
“anagram”, “same characters”, “rearrange” Valid Anagram (242), Group Anagrams (49) HashMap / int[26]
“frequency”, “how many times”, “count” Top K Frequent (347) HashMap (value -> count)
“group … by”, “cluster” Group Anagrams (49) HashMap (key -> list)
“every other element”, “product of all except self” Product Except Self (238) prefix/postfix pass
“valid”, “no repeats in row/col/box” Valid Sudoku (36) HashSet per group
“consecutive”, “sequence” (must be O(n)) Longest Consecutive (128) HashSet of all values

If the problem gives you an unsorted array and asks a question about existence, counting, or grouping, this is almost always the right pattern. If the array is sorted, consider Two Pointers (Pattern 2) or Binary Search (Pattern 5) first – they avoid the extra space.


General pseudocode template

This single shape solves six of the eight problems in this section. Learn it, then adapt the lookup key and the stored value to each problem.

function solve(input):
    create an empty hash structure

    for each element x in input:
        key  <- transform(x)            # the value, count, signature, or complement to look up
        if key is already in the hash structure:
            use the stored info to answer        # found a pair, a duplicate, a group, ...
            (optionally return early)
        otherwise:
            store info about x under key         # index, count, list, ...
            (or store x under transform(x))

    return the accumulated answer

Variants you will see:


Problems in this section

Eight problems, ramping Easy -> Medium. Do them in order – each one introduces one new idea.

# Folder Problem Difficulty One-line teaser
1 0217-contains-duplicate Contains Duplicate Easy Is any value repeated? A HashSet beeps on the second sighting.
2 0242-valid-anagram Valid Anagram Easy Do two strings have the same letter counts? Add for one string, subtract for the other.
3 0001-two-sum Two Sum Easy Find two indices summing to target; store each value’s index as you go.
4 0049-group-anagrams Group Anagrams Medium Sort each word to get a key; group all words that share a key.
5 0347-top-k-frequent Top K Frequent Elements Medium Count with a map, then bucket values by frequency for an O(n) top-K.
6 0238-product-of-array-except-self Product of Array Except Self Medium Multiply prefix products by postfix products – no division allowed.
7 0036-valid-sudoku Valid Sudoku Medium One HashSet per row, column, and 3x3 box catches every duplicate.
8 0128-longest-consecutive-sequence Longest Consecutive Sequence Medium Drop all values into a set; only expand from sequence starts.

Common pitfalls of the pattern

Hashing is powerful but has sharp edges. Beginners hit these repeatedly:

Pattern Mastery Quiz

Five questions ramping from recall to design. Try each before revealing.

Q1 (recall). In one sentence, what is the core trade this pattern makes?

Show answer Trade memory for time: spend O(n) extra space on a hash structure so each "have I seen / how many / where" question is answered in O(1) instead of O(n).

Q2 (pattern recognition). A new problem: “given a list of words, find all words that appear more than once.” Which tool fits best?

Show answer **(b)** -- you need the COUNT, not just existence, so a map word -> count is the right shape. (a) can only say "seen / not seen", losing the count.

Q3 (pattern recognition). Problem: “is every value in this array distinct?” Which is the most direct tool?

Show answer **(a)** -- a set answers existence in O(1); the boolean return of `add` detects the first repeat. This is literally Contains Duplicate.

Q4 (apply). You’re solving Top K Frequent with the bucket method on nums = [5,5,5,5,6,7], k = 1. Which slot holds the answer value, and what is returned?

Show answer **(a)** -- 5 appears 4 times so it lands in `bucket[4]`; the top-down scan hits slot 4 first and returns `[5]`.

Q5 (design). Sketch (in words, not code) how to solve “first non-repeating character in a string” using ideas from this pattern.

Show answer Two passes: pass 1 builds a char -> count map; pass 2 walks the string again and returns the first character whose count is 1. The map is the hashing habit; the second pass preserves original order to find the FIRST unique.

With those in mind, open 0217-contains-duplicate and start.