Data Structures · Visualization
Hash Table: Get
Hash Table
Getting a value looks up the bucket for a given key using the same hash function used during insertion, then scans that (usually very small) bucket for the matching key. 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: Get
Hash TableGetting a value looks up the bucket for a given key using the same hash function used during insertion, then scans that (usually very small) bucket for the matching key. Because the hash function narrows the search down to essentially one bucket, this is extremely fast on average.
How Get Works
- Compute the hash of the key and convert it to a bucket index.
- Go directly to that bucket.
- Scan the (typically short) list of entries in the bucket for a matching key.
- Return the associated value if found, or 'not found' otherwise.
Get Worked Example
Input: Get value for key 'name'
- hash('name') % size = 3
- Look in bucket 3
- Find entry ('name', 'Sam') — match!
Output: 'Sam'
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: get 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.
Get Time & Space Complexity
When to Use Get
- Looking up a user's data by their ID
- Checking if a cached result already exists before recomputing it
- Fast dictionary/vocabulary lookups
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.