Study Atlas › Computer Science › Data structures

Hash tables

Store key–value pairs with average O(1) lookup by computing an index from a hash of the key.

Edited by Alex C, EduAlx · Sources cited below

Explanation

Collisions are handled by chaining or open addressing. Load factor controls performance; tables resize when it gets too high. Hashing was described by Hans Peter Luhn at IBM (1953). Python dict, Java HashMap and database indexes use it.

Example

Counting word frequencies in a document in one pass.

Code

counts = {}
for w in text.split():
    counts[w] = counts.get(w, 0) + 1

How it works, step by step

  1. Compute hash(key)
  2. Map hash to a bucket index
  3. Store or look up in the bucket
  4. Resolve collisions; resize when load is high

Open the interactive page with diagrams →

People behind it

References

  1. W. W. Peterson, IBM J. Res. Dev. 1957, 1, 130–146.

Textbooks

Related topics

Trees and binary search treesData structures: arrays, linked lists, stacks, queuesHeaps and priority queuesGraph algorithms: BFS, DFS and Dijkstra's shortest pathSorting algorithms (quicksort, mergesort, heapsort)Dynamic programmingAlgorithms and Big-O complexityTuring machines and computabilityError-detecting and error-correcting codesP versus NP