# What Is Meant by Collision in Data Structure?

//

Larry Thompson

What Is Meant by Collision in Data Structure?

Collision is a term used in data structures to describe a situation where two or more elements of a hash table or hash map are assigned to the same location, known as a hash bucket. In other words, collision occurs when two different keys hash to the same index in the underlying data structure.

## Understanding Hash Tables

To better understand collisions, let’s first take a look at hash tables. A hash table is a data structure that allows efficient insertion, deletion, and search operations. It consists of an array of buckets or slots, where each slot can store one element.

To determine the index where an element should be stored in the array, a hash function is used. The hash function takes the key of the element as input and calculates an index based on it. Ideally, each key should have a unique index in the array.

## The Problem with Collisions

However, due to the limited size of the array and infinite possible keys, collisions are bound to happen. When two different keys result in the same index after applying the hash function, we have a collision.

Collisions can lead to several issues:

• Data Loss: If two elements collide and attempt to occupy the same slot in the array, one of them will overwrite the other. This leads to data loss as one element replaces another.
• Decreased Performance: Collisions can slow down operations performed on a hash table since additional steps are required to handle them. The time complexity for inserting or retrieving an element from a hash table increases when collisions occur frequently.

## Collision Resolution Techniques

To handle collisions effectively and maintain the performance of hash tables, several collision resolution techniques have been devised:

### 1. Separate Chaining

In separate chaining, each bucket in the hash table contains a linked list of elements that collided. When a collision occurs, the new element is simply appended to the linked list at that index. This technique ensures that all collided elements are stored and accessible.