Problem
Design a HashMap without using any built-in hash table libraries.
Implement the MyHashMap
class:
MyHashMap()
initializes the object with an empty map.void put(int key, int value)
inserts a(key, value)
pair into the HashMap. If thekey
already exists in the map, update the correspondingvalue
.int get(int key)
returns thevalue
to which the specifiedkey
is mapped, or-1
if this map contains no mapping for thekey
.void remove(key)
removes thekey
and its correspondingvalue
if the map contains the mapping for thekey
.
Examples
Example 1:
|
|
Constraints
0 <= key, value <= 10^6
- At most
10^4
calls will be made toput
,get
, andremove
.
Solution
Method 1 - Chaining (Linked List Buckets)
The map is generally an array-based structure. If we store only one value at each array index, we may have issues with collisions. To handle collisions, we use chaining with linked lists. This is the classic approach for integer keys .
Key points:
- Use a hash function to map keys to array indices.
- Each array index points to a linked list of entries (for collision resolution).
put
,get
, andremove
operations traverse the linked list at the hashed index.
Pseudocode
- Compute hash of key to get array index.
- If no entry at index, insert new node.
- If entry exists, traverse linked list:
- If key exists, update value.
- Else, append new node at end.
- For get/remove, traverse list at hashed index.
|
|
Approach
Nnw we need hash function, we can use Integer.hashCode() on key.
- Put call - Each time from hash-code, we go to index in array. If we don’t see any value there, that is no collision and we simply add the values to map. If there is a collision, there are 2 cases -
- same key is added to map, so nothing to do just set the latest value.
- different key is added to map, so just appended it to linked list
- Get call - Use hash function, and get the value. Traverse list and get the values
- Remove call - Use hash function, get the array index and remove the key from linked list
Code
|
|
|
|
Method 2 - Open Addressing (Linear Probing)
This method demonstrates a hash table implementation using open addressing (linear probing) and a custom hash function. This is a more educational approach, showing how hash tables can be implemented from scratch for string keys or for learning purposes.
Key points:
- Uses a custom hash function (example for string keys).
- Handles collisions with linear probing.
- Includes logic for checking if the table is full.
Pseudocode
- Compute hash of key to get array index.
- If slot is empty, insert.
- If slot is occupied and key matches, update value.
- If slot is occupied and key does not match, move to next slot (linear probing).
- For get/search, probe until key is found or all slots checked.
Notes
- The chaining method is a direct solution for Leetcode 706 (integer keys, linked list buckets).
- The open addressing method is a general-purpose educational hash table for string keys, using linear probing.
- Both methods illustrate core hash table concepts and can be compared for learning.
Probability of Collision
Given 100 items, the probability of collision for:
- 23 items is ~50%
- 50 items is 97%
- 70 items is 99.9%
Code
|
|
Probability of Collision
Given 100 items, the probability of collision for:
- 23 items is ~50%
- 50 items is 97%
- 70 items is 99.9%