# Designing a HashMap with out Constructed-in Libraries

Design a HashMap with out utilizing any built-in hash desk libraries. To be particular, your design ought to embrace these capabilities:

• put(key, worth): Insert a (key, worth) pair into the HashMap. If the worth already exists within the HashMap, replace the worth.
• get(key): Returns the worth to which the required key’s mapped, or -1 if this map incorporates no mapping for the important thing.
• take away(key): Take away the mapping for the worth key if this map incorporates the mapping for the important thing.

Examples:

Enter: n = 8

• put(1, 1)
• put(2, 2)
• get(1)
• get(3)
• put(2, 1)
• get(2)
• take away(2)
• get(2)

Output:
1
-1
1
-1

Rationalization: MyHashMap hashMap = new MyHashMap();
hashMap.put(1, 1);
hashMap.put(2, 2);
hashMap.get(1); // returns 1
hashMap.get(3); // returns -1 (not discovered)
hashMap.put(2, 1); // replace the prevailing worth
hashMap.get(2); // returns 1
hashMap.take away(2); // take away the mapping for two
hashMap.get(2); // returns -1 (not discovered)

Enter: n = 8

• put(1, 1)
• put(2, 2)
• get(1)
• get(2)
• put(3, 1)
• get(3)
• take away(2)
• take away(3)

Output:
1
2
1

Method: To resolve the issue observe the beneath thought:

We are going to use array dimension upto 1e6. We are going to initialize all of the values of the array by -1, as a worth to indicate no component at the moment at this place. Thus, we are able to use this array for all the capabilities talked about above.

Steps that had been to observe the above strategy:

• There could be at max 10^4 key-value pairs so we create an array of dimension 10^4+ 1 (0-based indexing).
• We initialize all of the array values with -1 as a result of by default they’re 0 in an empty array and our price for a selected key will also be 0. So if we don’t initialize with one thing else it should give us the unsuitable outputs.
• After that, the whole lot is a chunk of cake. We put() the given worth on the specified index key and equally get() the worth saved on the index key.
• When eradicating, we simply initialize the required index key again to -1.

Under is the code to implement the above steps:

## Java

 `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` `worth)` `    ``{` `        ``mapArray[key] = worth;` `    ``}` ` `  `    ``public` `int` `get(``int` `key) { ``return` `mapArray[key]; }` ` `  `    ``public` `void` `take away(``int` `key) { mapArray[key] = -``1``; }` ` `  `    ` `    ``public` `static` `void` `foremost(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.take away(``2``);` `        ``System.out.println(hashMap.get(``2``));` `    ``}` `}`

Time Complexity: O(1)
Auxiliary House: O(1)