Complete-Preparation

🎉 One-stop destination for all your technical interview Preparation 🎉

View the Project on GitHub

706. Design HashMap 🌟

Design a HashMap without using any built-in hash table libraries.

Implement the MyHashMap class:

Array implementation


class MyHashMap {
public:
    int arr[1000001];
    MyHashMap(){
        memset(arr, -1, sizeof(arr));
    }

    void put(int key, int val){
        arr[key] = val;
    }

    int get(int key){
        return arr[key];
    }

    void remove(int key){
        arr[key] = -1;
    }
};

With the hashing function - 1

Code

class MyHashMap {
    vector<list<pair<int, int>>> mp;
    int sz;

public:
    MyHashMap()
    {
        sz = 119;
        mp.resize(sz);
    }

    // hash function
    int hash(int key)
    {
        return key % sz;
    }

    // returns iterator to the element with key == k
    list<pair<int, int>>::iterator search(int key)
    {
        int i = hash(key);
        list<pair<int, int>>::iterator it = mp[i].begin();
        while (it != mp[i].end()) {
            if (it->first == key) {
                return it;
            }
            it++;
        }
        return it;
    }

    // insert key, value pair
    void put(int key, int value)
    {
        int i = hash(key);
        remove(key);
        mp[i].push_back({ key, value });
    }

    // get value for key
    int get(int key)
    {
        int i = hash(key);
        list<pair<int, int>>::iterator it = search(key);
        if (it != mp[i].end()) {
            return it->second;
        }
        return -1;
    }

    // remove key, value pair
    void remove(int key)
    {
        int i = hash(key);
        list<pair<int, int>>::iterator it = search(key);
        if (it != mp[i].end()) {
            mp[i].erase(it);
        }
    }
};

With the hashing function - 2


struct Node {
public:
    int key, val;
    Node* next;
    Node(int k, int v, Node* n)
    {
        key = k;
        val = v;
        next = n;
    }
};
class MyHashMap {
public:
    const static int size = 19997;
    const static int mult = 12582917;
    Node* data[size];
    int hash(int key)
    {
        return (int)((long)key * mult % size);
    }
    void put(int key, int val)
    {
        remove(key);
        int h = hash(key);
        Node* node = new Node(key, val, data[h]);
        data[h] = node;
    }
    int get(int key)
    {
        int h = hash(key);
        Node* node = data[h];
        for (; node != NULL; node = node->next)
            if (node->key == key)
                return node->val;
        return -1;
    }
    void remove(int key)
    {
        int h = hash(key);
        Node* node = data[h];
        if (node == NULL)
            return;
        if (node->key == key)
            data[h] = node->next;
        else
            for (; node->next != NULL; node = node->next)
                if (node->next->key == key) {
                    node->next = node->next->next;
                    return;
                }
    }
};

Must Read