HASHING
Hashing
Hashing is a technique used in computer science to efficiently map data from a larger space to a smaller fixed-size space, known as a hash table or hash map. The mapping process involves the use of a hash function, which takes an input (or "key") and returns an integer value (called a hash code or hash value). This hash value is then used to index an array or a hash table, allowing for quick lookups, insertions, and deletions.
Key Concepts in Hashing:
Hash Function:
A hash function is an algorithm that takes an input (like a string, number, or object) and returns a fixed-size integer, known as a hash code. The hash function should distribute keys uniformly across the hash table to minimize collisions (when two keys map to the same hash value).
A hash table (or hash map) is a data structure that uses a hash function to map keys to values. The keys are hashed into indices of an array, where the values are stored.
The key advantage of hash tables is that they offer O(1) average time complexity for lookups, insertions, and deletions, making them extremely efficient for these operations.
Collision:
A collision occurs when two different keys produce the same hash code (i.e., map to the same index in the hash table). Since a hash table can only store one value at each index, this is a problem that must be handled properly.