jayati

Design HashSet

May 30th, 2023
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. class MyHashSet {
  2. public:
  3.      unordered_set<int> s;
  4.     MyHashSet() {
  5.        
  6.     }
  7.    
  8.     void add(int key) {
  9.         s.insert(key);
  10.        
  11.     }
  12.    
  13.     void remove(int key) {
  14.         s.erase(key);
  15.        
  16.     }
  17.    
  18.     bool contains(int key) {
  19.         if(s.find(key)!=s.end())
  20.         {
  21.             return true;
  22.         }
  23.         else
  24.         {
  25.             return false;
  26.         }
  27.     }
  28. };
  29.  
  30. /**
  31.  * Your MyHashSet object will be instantiated and called as such:
  32.  * MyHashSet* obj = new MyHashSet();
  33.  * obj->add(key);
  34.  * obj->remove(key);
  35.  * bool param_3 = obj->contains(key);
  36.  */
Advertisement
Add Comment
Please, Sign In to add comment