Learn

Hash Tables: Finding Values by Key

Explore hashing, collisions, equality checks, and the tradeoffs behind fast key-based lookup.

Turn a key into a place to look

A hash table stores key-value associations. A hash function maps a key toward a location in a table, allowing a lookup to inspect a small relevant area rather than scan every stored record. This is useful for jobs such as looking up an item by its identifier or counting occurrences of a name.

The key property is not that every key gets a unique hash. Different keys can map to the same location. A correct table has a collision strategy and checks the actual key before returning a value. Hashing narrows the search; equality determines whether the requested key was found.

Build an illustrative tiny table

Imagine a toy table with seven buckets and integer keys. Use the remainder after division by seven to select a bucket. Key 16 goes to bucket two, and key 23 also goes to bucket two. They collide. With chaining, that bucket holds both key-value pairs, and a lookup checks their keys until it finds the requested pair.

This toy function is easy to trace but should not be mistaken for a production hash-table design. Real keys may be strings, and real implementations make choices about mixing, resizing, storage layout, and resistance to troublesome inputs. The example’s purpose is to make collisions visible, not recommend an implementation.

Expected constant time needs assumptions

MIT’s hashing notes analyze chaining in terms of the number of stored keys, bucket count, and distribution assumptions. With suitable distribution and a controlled load factor, expected lookup cost can remain constant with respect to the number of keys. If everything collides, a chain can instead require scanning many entries.

“Constant time” also does not mean zero cost or the same number of nanoseconds for every operation. Computing a hash for a long key, comparing candidate keys, allocating storage, and accessing memory all contribute work. Some operations trigger resizing, which reorganizes the table. Amortized or expected statements need their qualifiers.

Follow a word-count task

Suppose the input words are amber, blue, amber, green, blue, amber. Start with no counts. For each word, look up its current count, treating an absent key as zero, then store one more. The final associations are amber to three, blue to two, and green to one.

The output answers a key-based question without preserving every position where a word appeared. If positions are needed later, the value must contain more information. Python’s dictionary tutorial documents the language’s mapping interface. That interface is useful to learn separately from a specific hash-table storage design.

Keys and order are separate concerns

A key must remain compatible with the table’s hashing and equality rules while it is stored. Changing the information that determines its hash can make its original location inappropriate. Libraries therefore impose rules on usable keys. Two objects that compare equal must behave consistently under the applicable hashing contract.

Do not assume that hash-based storage provides sorted order or efficient range queries. Asking for one identifier and asking for all timestamps between two dates are different problems. A sorted index or tree may better match the second question. Even where a language preserves insertion order, that is not numerical or alphabetical sorting.

Hash tables exchange additional storage and organization for convenient exact-key access. Evaluate memory use, update patterns, key lengths, collision behavior, and the operations you actually need. Read binary search for an alternative based on sorted order, and Big O for interpreting the qualifications in complexity claims.