Design HashSet LeetCode Solution

Problem – Design HashSet

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

Implement MyHashSet class:

  • void add(key) Inserts the value key into the HashSet.
  • bool contains(key) Returns whether the value key exists in the HashSet or not.
  • void remove(key) Removes the value key in the HashSet. If key does not exist in the HashSet, do nothing.

Example 1:

["MyHashSet", "add", "add", "contains", "contains", "add", "contains", "remove", "contains"]
[[], [1], [2], [1], [3], [2], [2], [2], [2]]
[null, null, null, true, false, null, true, null, false]

MyHashSet myHashSet = new MyHashSet();
myHashSet.add(1);      // set = [1]
myHashSet.add(2);      // set = [1, 2]
myHashSet.contains(1); // return True
myHashSet.contains(3); // return False, (not found)
myHashSet.add(2);      // set = [1, 2]
myHashSet.contains(2); // return True
myHashSet.remove(2);   // set = [1]
myHashSet.contains(2); // return False, (already removed)


  • 0 <= key <= 106
  • At most 104 calls will be made to addremove, and contains.

Design HashSet LeetCode Solution in Python

class MyHashSet: 
    def eval_hash(self, key):
        return ((key*1031237) & (1<<20) - 1)>>5

    def __init__(self):
        self.arr = [[] for _ in range(1<<15)]

    def add(self, key: int) -> None:
        t = self.eval_hash(key)
        if key not in self.arr[t]:

    def remove(self, key: int) -> None:
        t = self.eval_hash(key)
        if key in self.arr[t]:

    def contains(self, key: int) -> bool:
        t = self.eval_hash(key)
        return key in self.arr[t]

Design HashSet LeetCode Solution in Java

class MyHashSet {
    int num[];
    public MyHashSet() {
        num = new int[31251];
	// set the bit if that element is present
    public void add(int key) {
	//unset the bit if a key is not present
    public void remove(int key) {
        num[getIdx(key)] &= (~getMask(key));
	//check if bit corresponding to the key is set or not
    public boolean contains(int key) {
        return (num[getIdx(key)]&getMask(key))!=0;
	// idx of num[] to which this key belongs
	// for key = 37, it will give 1
    private int getIdx(int key)
        return (key/32);
	// get mask representing the bit inside num[idx] that corresponds to given key.
	// for key = 37, it will give 00000000000000000000000000100000
    private int getMask(int key)
        return (1<<key);

Design HashSet LeetCode Solution in C++

class MyHashSet {
	int prime;
	vector<list<int>> table;

	int hash(int key) {
		return key % prime;

	list<int>::iterator search(int key) {
		int h = hash(key);
		return find(table[h].begin(), table[h].end(), key);

	MyHashSet() : prime(10007), table(prime) {}
	void add(int key) {
		int h = hash(key);
		if (!contains(key))
	void remove(int key) {
		int h = hash(key);
		auto it = search(key);
		if (it != table[h].end())
	bool contains(int key) {
		int h = hash(key);
		return search(key) != table[h].end();
Design HashSet LeetCode Solution Review:

In our experience, we suggest you solve this Design HashSet LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Design HashSet LeetCode Solution

Find on LeetCode


I hope this Design HashSet LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *