A hash function is a function assigning each key in an input set a value in an output set . Usually is much smaller: a long name or large integer becomes a small bucket index. Equal keys must receive equal values from the same hash function; different keys may also receive equal values. Such a hash collision means "look in the same place", and does not establish key equality. If , the pigeonhole principle makes some collisions unavoidable.
For example, modular arithmetic gives the hash function , with seven bucket indices . Store the seven keys : their values are respectively . In particular, although . A hash table is a data structure that uses these indices to locate records. With separate chaining, each bucket holds a linked list of records, so bucket retains all four keys . Inserting must not overwrite merely because their hash function values agree.
Figure 1.
Seven keys stored in seven buckets using the hash function h(k) = k mod 7
. Orange arrows identify four colliding keys. Bucket 3 keeps all four records in a linked list; equality is checked on the stored keys. Details.
Searching for computes bucket , then compares the actual keys until it reaches . Searching for absent key reaches that same bucket and must reject every stored key. This data structure trades a search through all records for a search through one short chain, provided the hash function distributes keys adequately. A fixed hash function can perform poorly on patterned keys: already demonstrate the problem. A universal hash family introduces controlled randomness to limit the expected value of this extra work. Neither indexing nor a small collision probability inherently supplies encryption or message authentication; a cryptographic hash function has additional requirements such as collision resistance.

Articles by others on the same topic (NaN)

A hash function is a mathematical algorithm that takes an input (or "message") and produces a fixed-size string of bytes, typically in the form of a hash value or hash code. The output is usually a numerical representation of the original data, and it is designed to uniquely correspond to the input data. Here are some key characteristics and properties of hash functions: 1. **Deterministic**: For a given input, a hash function will always produce the same output.
Hash function by Ciro Santilli 40 Updated 2025-07-16
Applications: