Trusted answers to developer questions

What is chaining in hash tables?

Free System Design Interview Course

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2024 with this popular free course.

Chaining is a technique used for avoiding collisions in hash tables.

A collision occurs when two keys are hashed to the same index in a hash table. Collisions are a problem because every slot in a hash table is supposed to store a single element.

The chaining technique

In the chaining approach, the hash table is an array of linked lists i.e., each index has its own linked list.

All key-value pairs mapping to the same index will be stored in the linked list of that index.

svg viewer

The benefits of chaining

  • Through chaining, insertion in a hash table always occurs in O(1) since linked lists allow insertion in constant time.

  • Theoretically, a chained hash table can grow infinitely as long as there is enough space.

  • A hash table which uses chaining will never need to be resized.

RELATED TAGS

hash tables
data structures
hashing
chaining
Copyright ©2024 Educative, Inc. All rights reserved
Did you find this helpful?