1. Lookup map
Create a Map so ids can point directly to items.
const byId = new Map()Typorithm · easy
Step 1 of 8 · intro card
Search Algorithms
Hash Table Lookup builds a key-value index so a later search can jump directly to a matching item.
Repeated linear searches can be replaced by one indexing pass.
A Map stores each item under its id.
After the index exists, lookup by id is expected O(1).
This trades extra memory for faster repeated searches.
Build an index with Map.
Use stable keys like item.id.
Lookup with byId.get(targetId).
Return null for missing keys.
Expected lookup time is O(1).
function hashTableLookup(items, targetId) {
const byId = new Map();
for (const item of items) {
byId.set(item.id, item);
}
return byId.get(targetId) ?? null;
}Connect each part of the code to the concept before you type.
Create a Map so ids can point directly to items.
const byId = new Map()Build the lookup table once.
for (const item of items)Use item.id as the key and the item as the value.
byId.set(item.id, item)Get the target by key, or return null when missing.
byId.get(targetId) ?? nullRead the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputKeys: Ada, Grace, Linus, Ken · lookup Grace
Follow temporary values, local state and supporting structures at this step.
Local state · indices and saved values
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state