In data structure, a collision occurs when two or more values to be hashed by a particular hash function hash to the same slot in the table or data structure being generated by the hash function. Hashing is a technique of mapping a large chunk of data into small tables using a hashing function, which is also known as the message digest function. The hash function is used to find the index of the array, and the hash value is used to create an index for the key in the hash table. The hash function may return the same hash value for two or more keys, and when two or more keys have the same hash value, a collision happens.
There are two common ways to deal with collisions: chaining and open addressing. Chaining is one of the most popular and commonly used techniques to handle collisions. When we use chaining to resolve collisions, we simply allow each slot in the hash table to accept more than one value. Therefore, when multiple elements are hashed into the same slot index, these elements are inserted into a singly-linked list which is known as a chain. Open addressing is another technique to handle collisions, which involves finding an alternate location when a collision happens.
In summary, a collision in data structure occurs when two or more values to be hashed by a particular hash function hash to the same slot in the table or data structure being generated by the hash function. There are two common ways to deal with collisions: chaining and open addressing.