Hash tables are also called hash maps, maps, unordered maps, or dictionaries. A hash table is a structure that maps keys to values. This makes them extremely efficient for lookups because if you have the key, retrieving the associated value is a constant-time operation.
Hash tables have fast lookups (O(1)
) on average. However, in the worst case, they have slow (O(n)
) lookups. The slow lookups happen when there is a hash collision (two different keys hash to the same index).
Hash tables have fast insertions (O(1)
) on average. However, in the worst case, they have slow (O(n)
) insertions. Just like with the lookups, the worst case occurs due to hash collisions.
Hash tables have fast deletes (O(1)
) on average. However, in the worst case, they have slow (O(n)
) deletions. Just like with lookups and insertions, the worst case occurs due to hash collisions.