Data Structures · Visualization

Hash Table: Delete

Hash Table

Deleting removes a key-value pair from the table. Time: O(1) average, O(n) worst case. Space: O(1).

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

Hash Table: Delete

Hash Table

Deleting removes a key-value pair from the table. Like get, it hashes the key to find the right bucket, then removes the matching entry from that bucket's small list, leaving other entries in the bucket untouched.

How Delete Works

  1. Compute the hash of the key and convert it to a bucket index.
  2. Go to that bucket.
  3. Search the bucket's entries for a matching key.
  4. If found, remove that entry from the bucket.

Delete Worked Example

Input: Delete key 'name' from the table

  1. hash('name') % size = 3
  2. Look in bucket 3, find ('name', 'Sam')
  3. Remove that entry from the bucket

Output: bucket[3] = [] (entry removed)

Try your own input
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: delete key=name
Input: Delete key 'name' from the table

Behind the Scenes Execution

Press ▶ Run in the visualizer — the call stack, registers and memory will replay here in sync with the code, line by line.

Delete Time & Space Complexity

TimeO(1) average, O(n) worst case
SpaceO(1)

When to Use Delete

  • Removing a user account from a user-ID lookup table
  • Evicting an expired entry from a cache
  • Removing a completed item from a tracking set

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.