Algorithms And Data Structures Codexery

Hash table

A data structure mapping keys to values via hash functions.

Hash table

A hash table is a data structure in computer science that implements an associative array, mapping keys to values using a hash function to compute an index into an array of buckets. It is widely used for efficient lookup, insertion, and deletion operations, with average constant time complexity in well-dimensioned designs.

field
Computer science
known_for
Implementing associative arrays with hash functions, enabling efficient key-value storage and retrieval
key_concepts
Hash function, hash collision, chaining, open addressing, load factor

Lore & Background

The idea of hashing arose independently in different places. The first example of open addressing was proposed by A. D. Lin, building on Luhn's memorandum. Around the same time, Gene Amdahl, Elaine M. Open addressing with linear probing is credited to Amdahl, although Andrey Ershov independently had the same idea. The term 'open addressing' was coined by W.

Reader's Guide

Hash tables are a fundamental data structure in computing, offering average constant-time performance for lookups, insertions, and deletions when properly dimensioned. They are widely used in many kinds of computer software, particularly for associative arrays, database indexing, caches, and sets. Many programming languages provide built-in hash table structures, such as Python’s dictionaries, Java’s HashMap, C++’s unordered_map, and Go maps, which abstract the complexity of hashing from the programmer. The efficiency of a hash table depends on the load factor, defined as the ratio of the number of stored elements to the number of available slots. Lower load factors generally yield faster operations. Common collision resolution strategies include chaining, which stores multiple elements in the same slot using linked lists, and open addressing, which searches for the next available slot according to a probing sequence. Hash tables exemplify a space–time tradeoff: with infinite memory, the entire key can be used directly as an index; with infinite time, values can be stored without regard for keys and retrieved via search. In many situations, hash tables are on average more efficient than search trees or any other table lookup structure.

Did You Know?

The Core Mechanism of Key-to-Value Mapping

A hash table is the concrete implementation behind what programmers commonly call a dictionary, map, or associative array. At its heart, the structure maintains a collection of unique keys, each paired with an associated value, and supports three fundamental operations: insertion, deletion, and lookup. The magic lies in a hash function that transforms an arbitrary key into a numeric index into a fixed-length array of buckets. If the array holds m slots and currently stores n elements, with m always at least n, the function guarantees the computed index falls within valid bounds. During a search, the key is fed through the hash function, the resulting position is examined, and the stored key at that slot is compared against the query to confirm a match before the value is returned. Storing the key alongside its value at every slot is essential, because collisions can place multiple entries at the same index. Beyond key-value pairs, the same structure can represent a set simply by tracking which keys are present and discarding the value component entirely.

Taming Collisions: Chaining and Open Addressing

Because virtually every practical hash function is imperfect, two distinct keys will occasionally map to the same bucket index, producing what is known as a collision. Designers must therefore choose a strategy for resolving these overlaps. The most widely taught approach is chaining: each bucket carries a linked list, and every key that hashes to that slot is appended to the list, so a lookup simply walks the chain until the matching key is found. The alternative, open addressing, keeps all entries within the single array itself. When a collision occurs, the algorithm probes forward through the table following a predetermined sequence until it locates an empty slot. Linear probing, the simplest such sequence, was independently conceived by Gene Amdahl at IBM and by Andrey Ershov. The very phrase "open addressing" entered the literature through W. Wesley Peterson, who discussed searching in large files. Arnold Dumey is credited with the first published description of chaining using a remainder-modulo-prime hash function, while Konheim and Weiss later supplied the first theoretical analysis of linear probing behavior.

Performance, Load Factor, and the Space-Time Tradeoff

One of the defining strengths of a well-sized hash table is that the average cost of a single lookup remains essentially constant no matter how many entries the table currently holds. Insertions and deletions likewise enjoy amortized constant-time behavior under typical assumptions, giving hash tables better search, insert, and delete bounds than self-balancing binary search trees. This efficiency, however, rests on a deliberate space-time tradeoff. In the theoretical extreme of unlimited memory, one could use the full key as a direct array index and retrieve a value in a single access. Conversely, with unlimited time but minimal memory, values could be stored in arbitrary order and recovered via binary or linear search. In practice, the load factor—the ratio of occupied entries to total buckets—governs real-world speed. Statistically, for an ideally random hash function and large tables, the number of items per bucket follows a Poisson distribution whose mean equals the load factor. Software therefore monitors this ratio and triggers a resize or rehash whenever it approaches a predefined ceiling, keeping operations fast.

Independent Inventions and Ubiquitous Adoption

Hashing did not emerge from a single eureka moment. Almost simultaneously, A. D. The word "hashing" itself first appeared in print in an article by Robert Morris. Decades later, the technique became so deeply embedded in software that major languages ship built-in hash-table types as first-class citizens: Python exposes dictionaries, Java offers HashMap, C++ provides unordered_map, and Go supplies native maps. All of these hide the underlying collision resolution and rehashing logic from the programmer. The structure's reach extends well beyond simple key-value storage; it underpins database indexing, caching layers, and set implementations across the industry, making it one of the most pervasive data structures in modern computing.

Frequently Asked Questions

Who is Hash table?

Hash table is a computer science data structure that implements an associative array, using a hash function to translate keys into indices of an array of buckets where values are stored.

What are Hash table's powers/role?

It delivers average constant-time insertion, lookup, and deletion of key-value pairs, which is why it powers everything from in-memory caches to database indexing.

How does Hash table's story end?

When the load factor climbs too high and collisions degrade performance, Hash table must be resized and rehashed into a larger bucket array to restore its efficiency.

Why is Hash table important?

It provides the O(1) average-case operations that make large-scale data retrieval practical, forming the backbone of hash maps in virtually every major programming language.

What are Hash table's key allies?

Its core supporting cast includes the hash function, collision-handling techniques such as chaining and open addressing, and the load factor metric that governs when a resize is needed.

More in Algorithms And Data Structures 25-38

Elsewhere in the Algorithms And Data Structures universe

Spotted an error? Know more?

This is a living reference — every entry is fact-audited, and reader corrections feed straight into our audit queue. Suggest an edit · See this site's audit record

Comments

Loading…
Open in the interactive codex →