Understanding and Using
Hash Tables
What’s a hash table?
Why use a hash table?
* In the worst case, you might have to. More on this later.
What can you do with one?
Well, assume we have a JavaScript object (which is a hash table!).
let a = {}
We can...
Operation | Code |
Insert a key-value pair | a['key'] = 'value'; |
Retrieve a value with a key | let value = a['key']; |
Delete a key-value pair | delete a['key']; |
Hash Tables: All O(1) Operations
All operations on hash tables are O(1)!
Operation | Code | Time Complexity |
Insert a key-value pair | a['key'] = 'value'; | O(1) |
Retrieve a key at a value | let v = a['key']; | O(1) |
Delete a key | delete a['key']; | O(1) |
When are hash tables useful?
How do hash tables do it?
0 |
1 |
2 |
3 |
4 |
key | Grace Hopper |
val | New York City, NY |
When might you want to use a hash table?
Review questions
What’s next?