Data Structures · Visualization

Hash Table: Introduction

Hash Table

A hash table stores key-value pairs and uses a 'hash function' to convert each key into a number, which becomes an index into an underlying array. Time: O(1) average for insert/get/delete, O(n) worst case with many collisions. Space: O(n).

Big-O: Average O(1) per operation; worst case O(n) on collisions.

Hash Table: Introduction

Hash Table

A hash table stores key-value pairs and uses a 'hash function' to convert each key into a number, which becomes an index into an underlying array. This means you can jump almost directly to where a value is stored just by knowing its key, instead of searching through every item. When two different keys hash to the same index (a 'collision'), the table handles it, often by storing a small list of entries at that index (chaining).

How Hash Table: Introduction Works

  1. Choose an array ('buckets') of a fixed starting size.
  2. For each key, run it through a hash function to get a number.
  3. Reduce that number to a valid array index, usually with modulo (index = hash % arraySize).
  4. Store the key-value pair at that index (using chaining or another method to handle collisions).

Hash Table: Introduction Worked Example

Input: Store 'age' -> 30 in a table of size 5

  1. hash('age') might compute to, say, 372
  2. index = 372 % 5 = 2
  3. Store the pair ('age', 30) at bucket index 2

Output: bucket[2] = [('age', 30)]

📖 Introduction: this is an overview of the topic, so there's nothing to run here. Pick an operation from the side menu to see a live, step-by-step animation.
Hash Table: Introduction
Runs each key through a hash function to compute a bucket index, so lookups don’t need to scan every entry.
0
∅
1
∅
2
∅
3
∅
4
∅
5
∅
6
∅
Active Hash map Key
Hash Table: Introduction
Press ▶ Run to watch it step by step.
Python · Hash Tables in Code
# Hash map: key -> value, O(1) average
scores = {}
scores["alice"] = 90     # insert
v = scores.get("bob", 0) # get with default
del scores["alice"]      # delete

if "bob" in scores:      # membership
    print("present")
# example: intro key=age value=30

Hash Table: Introduction Time & Space Complexity

TimeO(1) average for insert/get/delete, O(n) worst case with many collisions
SpaceO(n)

When to Use Hash Table: Introduction

  • Implementing dictionaries, objects, and sets in most programming languages
  • Caching computed results for fast lookup (memoization)
  • Counting frequencies of items (word counts, duplicate detection)
  • Database indexing for fast record lookup

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits

Ready for the next step?

See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.