Designing a HashMap with out Constructed-in Libraries

0
9
Adv1


Adv2

Enhance Article

Save Article

Like Article

Enhance Article

Save Article

Like Article

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

  • 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 secret’s mapped, or -1 if this map comprises no mapping for the important thing.
  • take away(key): Take away the mapping for the worth key if this map comprises 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

Clarification: 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

Strategy: To resolve the issue observe the under concept:

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 price to indicate no aspect at the moment at this place. Thus, we are able to use this array for the entire features talked about above.

Steps that had been to observe the above method:

  • There might 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 would give us the flawed outputs.
  • After that, all the pieces 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 fundamental(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 Area: O(1)

Like Article

Save Article

Adv3