Hide sidebar

Hashing is a fundamental concept in computer science, used to map data of arbitrary size to a fixed-size value. This is accomplished using a hash function, which takes an input (or "key") and returns a fixed-size string of bytes, typically a "hash code" or "hash value."

Interactive Hash Map Visualization

The visualization below demonstrates a simple hash map that uses a basic hash function and resolves collisions using chaining. You can add, remove, and search for key-value pairs to see how the hash map works.

HashMap Visualization

Step 1 of 0
Current Action
Ready to start
Phase
Ready
Current Key
None
Hash Value
None
[0]
empty
[1]
empty
[2]
empty
[3]
empty
[4]
empty
[5]
empty
[6]
empty
Current Slot
Key-Value Pair
Chained Items
Empty Slot
HashMap Operations: Hash function maps keys to array indices. Collisions handled by chaining (storing multiple items per slot). Average time: O(1) for insert/search/delete, Worst case: O(n) when all items hash to same slot.
Hash Function: Sum of ASCII character codes modulo table size. Simple but effective for demonstration. Real implementations use more sophisticated hash functions to minimize collisions.

Hashing & Hash Maps

Hashing is the process of converting a given key into another value. A hash function is used to generate the new value. A hash map (or hash table) is a data structure that uses a hash function to map keys to their associated values.

Key Operations

  • Insert: Adds a key-value pair to the hash map.
  • Delete: Removes a key-value pair from the hash map.
  • Search: Retrieves the value associated with a given key.
Hash Map Implementation

# Using a dictionary as a hash map
hash_map = {}

# Insert
hash_map["key1"] = "value1"
hash_map["key2"] = "value2"

# Search
value = hash_map.get("key1")  # "value1"

# Delete
del hash_map["key2"]