C++ ||one standard||one easy fast
Intuition\n Describe your first thoughts on how to solve this problem. \nC++ solution from scratch to create a Hash set\nFind a prime p\ndefine the hash functio
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nC++ solution from scratch to create a Hash set\nFind a prime p\ndefine the hash function\nh(key)=key%p\nConstruct a table with table[h(key)] as a list.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nLearning data structure and algorithm. This is a problem how to create a Hash set from scratch!\nA standard method to implement is definitive not easy. But the constraint " 0 <= key <= 10^6" makes it much easier!!\n\nDesigning a hash set was marked as easy by Leetcode. In addition to the standard method, a super simple method is provided, which can be completed in seconds, and shown in this short video!\n[https://youtu.be/T5W49vN4ssw](https://youtu.be/T5W49vN4ssw)\n\nHere I provide two different solutions. One is the more standard use of modulo primes as a hash function, with buckets consisting of keys with the same hash value. Another is a very simple solution using constraints!\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code \n```\nclass MyHashSet {\n//Find a prime p=30011,2381\npublic:\n    const int p=2381;\n    vector<list<int>> table;\n    MyHashSet() {\n        table.resize(p);\n    }\n    \n    void add(int key) {\n        int i=key%p;\n        for(int& x: table[i])\n            if(x==key) return ;\n        table[i].emplace_back(key);\n    }\n    \n    void remove(int key) {\n        int i= key%p;\n        for (auto it=table[i].begin(); it!= table[i].end(); it++) {\n            if (*it == key) {\n                table[i].erase(it);\n                return;\n            }\n        }\n    }\n    \n    bool contains(int key) {\n        int i=key%p;\n        for(int& x: table[i])\n            if(x==key) return 1;\n        return 0;\n    }\n};\n\n/**\n * Your MyHashSet object will be instantiated and called as such:\n * MyHashSet* obj = new MyHashSet();\n * obj->add(key);\n * obj->remove(key);\n * bool param_3 = obj->contains(key);\n */\n```\n# 2nd Solution using constraint 0<=key<=10^6, C++ bitset is good for implementation\n\n```\nclass MyHashSet {\n//Use bitset\npublic:\n    bitset<1000001> table;\n    MyHashSet() {\n        table.reset();\n    }\n    \n    void add(int key) {\n        table.set(key);\n    }\n    \n    void remove(int key) {\n        table.set(key, 0);\n    }\n    \n    bool contains(int key) {\n        return table[key];\n    }\n};\n\n/**\n * Your MyHashSet object will be instantiated and called as such:\n * MyHashSet* obj = new MyHashSet();\n * obj->add(key);\n * obj->remove(key);\n * bool param_3 = obj->contains(key);\n */\n```
['Hash Table', 'C++']