Design a HashMap without using any built-in hash table libraries. To be specific, your design should include these functions:
- put(key, value): Insert a (key, value) pair into the HashMap. If the value already exists in the HashMap, update the value.
- get(key): Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key.
- remove(key): Remove the mapping for the value key if this map contains the mapping for the key.
Examples:
Input: n = 8
- put(1, 1)
- put(2, 2)
- get(1)
- get(3)
- put(2, 1)
- get(2)
- remove(2)
- get(2)
Output:
1
-1
1
-1Explanation: MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // returns 1
hashMap.get(3); // returns -1 (not found)
hashMap.put(2, 1); // update the existing value
hashMap.get(2); // returns 1
hashMap.remove(2); // remove the mapping for 2
hashMap.get(2); // returns -1 (not found)Input: n = 8
- put(1, 1)
- put(2, 2)
- get(1)
- get(2)
- put(3, 1)
- get(3)
- remove(2)
- remove(3)
Output:
1
2
1
Approach: To solve the problem follow the below idea:
We will use array size upto 1e6. We will initialize all the values of the array by -1, as a value to denote no element currently at this position. Thus, we can use this array for all of the functions mentioned above.
Steps that were to follow the above approach:
- There can be at max 10^4 key-value pairs so we create an array of size 10^4+ 1 (0-based indexing).
- We initialize all the array values with -1 because by default they are 0 in an empty array and our value for a particular key can also be 0. So if we don't initialize with something else it will give us the wrong outputs.
- After that, everything is a piece of cake. We put() the given value at the specified index key and similarly get() the value stored at the index key.
- When removing, we just initialize the specified index key back to -1.
Below is the code to implement the above steps:
#include <iostream>
#include <vector>
using namespace std;
class MyHashMap {
vector<int> mapArray;
public:
MyHashMap() {
mapArray = vector<int>(1000001, -1);
}
void put(int key, int value) {
mapArray[key] = value;
}
int get(int key) {
return mapArray[key];
}
void remove(int key) {
mapArray[key] = -1;
}
};
int main() {
MyHashMap hashMap;
hashMap.put(1, 1);
hashMap.put(2, 2);
cout << hashMap.get(1) << endl; // Output : 1
cout << hashMap.get(3) << endl; // OUtput: -1 (not found)
hashMap.put(2, 1);
cout << hashMap.get(2) << endl; // Output: 1 (updated value)
hashMap.remove(2);
cout << hashMap.get(2) << endl; // Output: -1 (removed)
return 0;
}
// Java program to design HashMap
import java.io.*;
import java.util.*;
class MyHashMap {
int[] mapArray;
public MyHashMap()
{
mapArray = new int[1000001];
Arrays.fill(mapArray, -1);
}
public void put(int key, int value)
{
mapArray[key] = value;
}
public int get(int key) { return mapArray[key]; }
public void remove(int key) { mapArray[key] = -1; }
// Drivers code
public static void main(String args[])
{
MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
System.out.println(hashMap.get(1));
System.out.println(hashMap.get(3));
hashMap.put(2, 1);
System.out.println(hashMap.get(2));
hashMap.remove(2);
System.out.println(hashMap.get(2));
}
}
# python program to design HashMap
class MyHashMap:
def __init__(self):
self.mapArray = [-1] * 1000001
def put(self, key, value):
self.mapArray[key] = value
def get(self, key):
return self.mapArray[key]
def remove(self, key):
self.mapArray[key] = -1
# Driver code
if __name__ == "__main__":
hashMap = MyHashMap()
hashMap.put(1, 1)
hashMap.put(2, 2)
print(hashMap.get(1)) # Output: 1
print(hashMap.get(3)) # Output: -1 (not found)
hashMap.put(2, 1)
print(hashMap.get(2)) # Output: 1 (updated value)
hashMap.remove(2)
print(hashMap.get(2)) # Output: -1 (removed)
// C# program to design HashMap
using System;
public class MyHashMap {
private int[] mapArray;
public MyHashMap()
{
mapArray = new int[1000001];
Array.Fill(mapArray, -1);
}
public void Add(int key, int value)
{
mapArray[key] = value;
}
public int Get(int key) { return mapArray[key]; }
public void Remove(int key) { mapArray[key] = -1; }
static public void Main()
{
// Code
MyHashMap hashMap = new MyHashMap();
hashMap.Add(1, 1);
hashMap.Add(2, 2);
Console.WriteLine(hashMap.Get(1));
Console.WriteLine(hashMap.Get(3));
hashMap.Add(2, 1);
Console.WriteLine(hashMap.Get(2));
hashMap.Remove(2);
Console.WriteLine(hashMap.Get(2));
}
}
class MyHashMap {
constructor() {
this.mapArray = new Array(1000001).fill(-1);
}
put(key, value) {
this.mapArray[key] = value;
}
get(key) {
return this.mapArray[key];
}
remove(key) {
this.mapArray[key] = -1;
}
}
const hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
console.log(hashMap.get(1)); // Output: 1
console.log(hashMap.get(3)); // Output: -1 (not found)
hashMap.put(2, 1);
console.log(hashMap.get(2)); // Output: 1 (updated value)
hashMap.remove(2);
console.log(hashMap.get(2)); // Output: -1 (removed)
Output
1 -1 1 -1
Time Complexity: O(1)
Auxiliary Space: O(1)