Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 709 Bytes

36.md

File metadata and controls

33 lines (26 loc) · 709 Bytes
set<int>s;                  //use vector and mao for bettering optimal solution
    RandomizedSet() {
        
    }
    
    bool insert(int val) {
        if(s.find(val)!=s.end())
            return false;

        s.insert(val);
        return true;
    }
    
    bool remove(int val) {
        if(s.find(val)!=s.end())
        {
            s.erase(val);
            return true;
        }
        return false;
    }
    
    int getRandom() {
        // int t=rand()%s.size();
        // return s[t];
        int t = rand() % s.size();
    return *std::next(s.begin(), t);
    }