# Introduction and concepts

A **hash table**, also known as a **hash map**, is a **data structure** that enables **efficient** storage and retrieval of data through a **hash function**. It organizes data in **key-value pairs**, where each **key** is **unique** and associated with a corresponding **value**.

**Key-Value Storage**: Hash tables store data as unique key-value pairs for efficient retrieval.**Hash Function:**A hash function maps keys to array indices for storage and retrieval.**Fast Retrieval:**Hash tables quickly retrieve values based on keys (O(1) average time complexity).**Unique Keys**: Hash tables require keys to be unique to ensure proper functioning. If two keys are the same, the value associated with the latest key will overwrite the previous value.**Collision Handling:**Collisions, when two keys map to the same index, are handled using techniques like chaining or open addressing.**Efficiency Trade-Off:**Hash tables balance memory usage and retrieval speed based on the array size and collision resolution strategy.**Unordered Structure:**Hash tables do not guarantee a specific order for stored keys or values.**Dynamic Resizing:**Hash tables can dynamically resize to accommodate changes in the number of elements.**Memory Overhead:**Hash tables require additional memory for indices and collision resolution structures.

Animation for Hash Tables.

Question:"But you might be wondering, this hash function, doesn't it slow things down?"

**Answer:** You might have the concern that the **hash function**, which is somewhat like a black box, could potentially slow down the operations. However, the design of hash tables considers this to ensure optimal performance.

Underneath the hood, hash tables utilize **optimized hashing functions** that are specifically designed to be **fast**. These functions are implemented within various programming languages and frameworks, ensuring efficient execution.

It is worth noting that there are certain hash functions, such as **SHA-256,** which are **intentionally slower**. However, such complex hash functions are commonly used in specialized areas like **cryptography,** where the objective is to deliberately increase the time required for hashing.

In the context of hash tables, the chosen hash function is typically one that provides fast execution. The time complexity of hash table operations is commonly assumed to be **O(1)**, denoting constant time complexity and ensuring rapid performance.

Hence, **although the inner workings of the hash function might seem like a mysterious black box, the implementation of optimized and fast hash functions allows hash tables to deliver efficient and speedy operations.**

# Hash Operations

Operation | Time Complexity |

Insertion | O(1) |

Deletion | O(1) |

Retrieval | O(1) |

Update | O(1) |

Resizing | O(n) |

Collision Resolution | Varies (typically O(1) with a good hash function and load factor) |

## Collision

In this example, we are *inserting* names and phone numbers into a *hash table*. Initially, **John Smith** is *hashed* to address space **152** and stored along with its corresponding value. We continue adding names like *Lisa Smith*, *Sam Doe*, and *Sandra Oh*, but when we reach **Sandra Dee**, a *collision* occurs because it shares the *same address space* as John Smith. To handle collisions, a new data structure called *linked lists* is introduced. With hash tables, collisions are unavoidable as the data grows, and it can *slow down* accessing and inserting information.

Collisions in a hash table can lead to slower *reading* and *writing*, typically with a time complexity of **O(N/K)**.

There are several methods for collision resolution in hash tables:

**Separate Chaining**: This method involves**maintaining a linked list or another data structure at each index of the hash table.**Colliding keys are stored in separate buckets or nodes within the same index. This allows multiple keys to coexist at the same location.**Open Addressing**: With open addressing, all key-value pairs are stored directly in the hash table itself. If a collision occurs,**the algorithm probes for alternative (usually nearby) locations until an empty slot is found.**This method requires a careful selection of probing strategies, such as linear probing, quadratic probing, or double hashing.**Robin Hood Hashing**: In this method, colliding elements are placed in the same index, but an additional attribute called "distance" is used to keep track of how far each element has travelled from its original hashed position. During insertion, elements with shorter distances are shifted forward, making room for new elements and reducing clustering.**Cuckoo Hashing**: Cuckoo hashing uses multiple hash functions and multiple hash tables. Each key is hashed by different functions and placed in their respective hash tables. If a collision occurs, the algorithm checks the alternative hash table and swaps the keys if necessary. This process continues until all keys find their places or a predefined number of displacements is reached.

These are just a few examples of collision resolution methods commonly used in hash tables. The choice of method depends on factors such as expected data distribution, memory constraints, and performance requirements. Each method has its own trade-offs in terms of time complexity, memory usage, and ease of implementation.

"Hash tables are implemented differently in various programming languages, allowing key-value pairs of any data type. Objects in JavaScript have limitations on key types, but with ES6, maps and sets were introduced. Maps allow any data type as keys and maintain insertion order, while sets only store keys without values. These prebuilt data structures are variations of hash tables, providing different functionalities."