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 TableDeleting 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
- Compute the hash of the key and convert it to a bucket index.
- Go to that bucket.
- Search the bucket's entries for a matching key.
- If found, remove that entry from the bucket.
Delete Worked Example
Input: Delete key 'name' from the table
- hash('name') % size = 3
- Look in bucket 3, find ('name', 'Sam')
- Remove that entry from the bucket
Output: bucket[3] = [] (entry removed)
Press ▶ Run to watch it step by step.
# 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=nameBehind 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
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
Ready for the next step?
See the same ideas in action — visualize the algorithms that operate on these structures, or take a guided course.