classRandomizedSet { public: /** Initialize your data structure here. */ unordered_map<int,int> mp; // val -> index vector<int> a; random_device rd; RandomizedSet() { } /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */ boolinsert(int val){ if (mp.find(val) != mp.end()) returnfalse; a.push_back(val); mp[val] = a.size()-1; returntrue; } /** Removes a value from the set. Returns true if the set contained the specified element. */ boolremove(int val){ if (mp.find(val) == mp.end()) returnfalse; int index = mp[val]; if (index < a.size()-1) { mp[a.back()] = index; a[index] = a.back(); } mp.erase(val); a.pop_back(); returntrue; } /** Get a random element from the set. */ intgetRandom(){ return a[rd()%a.size()]; } };