Here's the signal: you catch yourself searching for something you've already seen. "Where did this number appear before?", "how many times have I met this letter?", "is its partner already here?" — each of those is a nested loop waiting to become a single map lookup. Almost every hash map question in interviews uses one of four patterns.
Whenever a question asks how many times, how many different, or which is most common, keep a map from each item to its count. One pass builds it, and every later question is a lookup. You'll use this in Count Distinct Elements in Every Window and Check If Array Pairs Are Divisible by K.
Members only · free account
The rest of this lesson, with 5 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson