Typorithm · easy

Hash Table Lookup

▷ Interactive algorithm labhash-table-lookupsearchmapdirect-lookupconstant-time
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Hash Table Lookup

0%

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.

Preparing algorithm lab…
01

Build an index with Map.

02

Use stable keys like item.id.

03

Lookup with byId.get(targetId).

04

Return null for missing keys.

05

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;
}

Read the implementation

Connect each part of the code to the concept before you type.

1. Lookup map

Create a Map so ids can point directly to items.

const byId = new Map()

2. Index items

Build the lookup table once.

for (const item of items)

3. Store by id

Use item.id as the key and the item as the value.

byId.set(item.id, item)

4. Direct lookup

Get the target by key, or return null when missing.

byId.get(targetId) ?? null

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Hash Table Lookup: Visual Walkthrough & Practice · Typorithm | ApplyDjinn