This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 1: Arrays and strings
Remember what you've seen so you never look for it twice.
The question to ask
Am I scanning the array again to find something I could have remembered on the first pass?
The brute force for most array questions is a nested loop: for each element, scan the rest for its partner, its duplicate, its group. A Map or Set turns that inner scan into an O(1) lookup, trading O(n) memory for dropping a whole factor of n. It is the first thing to try on almost any unsorted-array question, and the fallback when nothing else fits.
Want to watch it run first? Step through the interactive walkthrough.
Time O(n) average · Space O(n)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Top K Frequent without a heap: count with a Map, then drop values into buckets indexed by count. Counts can't exceed n, so the buckets are a fixed-size array and the whole thing is O(n).
function topKFrequent(nums: number[], k: number): number[] {
const counts = new Map<number, number>();
for (const n of nums) counts.set(n, (counts.get(n) ?? 0) + 1);
const buckets: number[][] = Array.from({ length: nums.length + 1 }, () => []);
for (const [value, count] of counts) buckets[count].push(value);
const result: number[] = [];
for (let count = buckets.length - 1; count > 0 && result.length < k; count--) {
result.push(...buckets[count]);
}
return result.slice(0, k);
}Longest Consecutive Sequence in O(n): a number begins a run only if number - 1 is absent. Every other number is skipped as a starting point, so each element is walked over at most twice in total.
function longestConsecutive(nums: number[]): number {
const values = new Set(nums);
let best = 0;
for (const n of values) {
if (values.has(n - 1)) continue;
let length = 1;
while (values.has(n + length)) length++;
best = Math.max(best, length);
}
return best;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Given an integer array, return true if any value appears at least twice, and false if every element is distinct. Contains duplicate, in short.
"Appears at least twice" is a seen-before check. Add to a Set as you go and stop on the first repeat.
Not attempted yet
Given two strings, decide whether one is an anagram of the other: the same letters with the same frequency, in any order.
"Anagram" means equal letter counts. One count array of 26, incremented for s and decremented for t.
Not attempted yet
Given an unsorted array of integers and a target, return the indices of the two numbers that add up to target. Exactly one answer exists.
Unsorted plus "indices of the two numbers" means a Map from value to index, looking up target minus the current value.
Not attempted yet
Return the element that appears more than n / 2 times in the array. It always exists.
"Appears more than" is a frequency count. The O(1)-space follow-up is Boyer–Moore voting, worth knowing by name.
Not attempted yet
Given an array of strings, group the anagrams together. Words in the same group use the same letters at the same frequency.
"Group" plus "anagram": a Map whose key is the sorted word (or a letter-count string) and whose value is the group.
Not attempted yet
Return the k most frequent elements of an integer array. Aim to beat O(n log n) time.
Count frequency with a Map. "Better than n log n" points at bucket sort by count; a size-k heap is the other accepted answer.
Not attempted yet
Check whether a partially filled 9 by 9 board is a valid sudoku: no digit repeats in any row, column or 3 by 3 box. Only filled cells matter.
"No digit repeats" in three kinds of unit is three families of Sets. The box index is Math.floor(r / 3) * 3 + Math.floor(c / 3).
Not attempted yet
Given an unsorted array, return the length of the longest consecutive sequence of integers. Your algorithm must run in O(n) time.
"Unsorted" and "in O(n) time" rule out sorting. A Set lets you start counting only at numbers with no predecessor.
Not attempted yet
Return the smallest missing positive integer from an unsorted array, in O(n) time and constant extra space.
"Missing positive" with O(1) space: the answer is in 1..n+1, so use the array itself as the hash set by swapping each value v to index v - 1.
Not attempted yet