Skip to main content

Hast Table

SpaceTime
AccessLookupInsertionDeletion
O(n)N/AO(1)O(1)O(1)

Definition​

Hash Table is a data structure used to store and retrieve data using a hash function.

Simplified

You have a big box of toys and you want to find your favorite toy car quickly next time you play. So, you decide to keep each toy in a specific spot in the box. For example, you always put your toy car in the top left corner. This way, the next time you want to play with it, you know exactly where it is and can grab it quickly without having to search through the whole box.

Practice​

AspectPseudo Code
Insertion
  insert(key, value):
index = key.hash_code() % HASH_MODULO
table[index] = Pair(key, value)

Search
 search(key):
index = key.hash_code() % HASH_MODULO
if table[index] != ø:
return table[index]
return ø
Deletion
delete(key):
index = key.hash_code() % HASH_MODULO
table[index] = null