Data Structures
Hash Map
Add: O(1) · Look up by key: O(1) · Delete: O(1). On average, all three are effectively instant.
The idea, in plain English
A hash map is like a coat check. You hand over your coat, which is the value. You get a numbered tag back, which is the key. Later, you show the tag and get your exact coat back right away. No one has to search through every coat.
How it works
- 1You store data as key → value pairs (like word → count).
- 2The map turns your key into a slot number behind the scenes.
- 3Look up, add, or update an item by its key. You never need to scan the whole map.
When you'd use it
Use a hash map for counting things, remembering settings by name, caching results, or looking things up by name or ID.
Common beginner mistakes
- Don't assume the keys stay in a sorted order. Don't rely on order in your logic.
- Remember to handle the case where a key doesn't exist. Don't forget the 'not found' case.
Try it — edit and run
Click the code to edit · press ⌘/Ctrl+↵ to run
Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.
Expected output — hit Run to try it
cat: 3
dog: 2
bird: 1See it in motion
Watch it hashHash mapAdd: O(1) · Look up by key: O(1) · Delete: O(1). On average, all three are effectively instant.
Keys to insert
Buckets (slot index)
0
empty
1
empty
2
empty
3
empty
4
empty
Ready. Press play to hash keys into buckets.
0/3
Placed
0/5
Buckets used
0
Collisions
0
Longest chain
Hashing / just placedChained (collision)Settled
Not sure this is the right topic? See the learning paths → or where this leads →