Codele / Data structures
Hash tables
How can a dictionary find a name instantly, without reading every entry? It turns the name into a number that points straight at one shelf.
- A hash table is how a dictionary finds things fast. The name is turned into a bucket number, and the thing is kept in that bucket.
- The recipe that makes the number is the hash function. The same name always gives the same number.
- To store or look up a name, work out its number and open only that bucket. No searching through the rest.
- Two names can get the same number. That is a collision, and the bucket keeps a short list of its names.
- Dictionaries and sets in most languages use a hash table inside.
Loading the playable lesson…