Skip to main content

Hash Maps & Sets

Hash Maps and Sets are data structures designed for fast lookups. They allow you to store and retrieve data in constant time on average β€” O(1) β€” by using a hashing mechanism instead of iterating through elements one by one.

They are especially powerful when the problem involves checking existence, counting occurrences, removing duplicates, or tracking visited elements. In many cases, a Hash Map or Set can turn an O(nΒ²) solution into O(n).

A hash works by transforming a value (key) into an index. This allows direct access to stored data without scanning the entire structure. Because of this, Hash Maps and Sets are often used as supporting structures to optimize algorithms.


Hash Maps​

A Hash Map stores data as key–value pairs. Each key is unique, and it maps directly to a value. This makes Hash Maps ideal for associating information, counting frequencies, or grouping data.

Instead of searching for a value manually, you can access it instantly using its key. This is why Hash Maps are commonly used for problems involving lookups, mappings, or aggregations.

Typical use cases include:

  • Counting how many times elements appear
  • Mapping one value to another
  • Tracking indices or positions
  • Caching results
const map = new Map<string, number>()

// add
map.set("a", 1)

// update
map.set("a", 2)

// get
map.get("a")

// has
map.has("a")

// delete
map.delete("a")

// size
map.size

// clear
map.clear()
// keys
for (const key of map.keys()) {}

// values
for (const value of map.values()) {}

// entries
for (const [key, value] of map.entries()) {}

// entries (shortcut)
for (const [key, value] of map) {}
// to array
Array.from(map)

// keys to array
Array.from(map.keys())

// values to array
Array.from(map.values())

Sets​

A Set is a collection of unique values. Unlike Hash Maps, Sets do not store key–value pairs β€” only values. Their main purpose is to efficiently check whether an element exists or to eliminate duplicates.

Sets are especially useful when the problem requires uniqueness guarantees or fast membership checks without caring about associated data.

Typical use cases include:

  • Removing duplicates from arrays
  • Checking if an element has been seen before
  • Validating uniqueness constraints
  • Tracking visited states
const set = new Set<number>()

// add
set.add(1)

// has
set.has(1)

// delete
set.delete(1)

// size
set.size

// clear
set.clear()
// values
for (const value of set.values()) {}

// keys (same as values)
for (const key of set.keys()) {}

// entries
for (const [value1, value2] of set.entries()) {}

// values (shortcut)
for (const value of set) {}
// to array
Array.from(set)

Why Hash Maps and Sets Are So Powerful​

The biggest advantage of Hash Maps and Sets is time efficiency. Operations like insert, delete, and lookup usually take constant time. This allows algorithms to scale much better compared to approaches that rely on nested loops.

If you find yourself repeatedly searching for values, comparing elements, or checking duplicates, a Hash Map or Set is often the right tool.

If a problem involves arrays or strings, frequency counting, duplicates, fast lookups, or tracking visited elements, Hash Maps and Sets are usually a strong candidate.