1
class RandomizedCollection {3
unordered_map<int, unordered_set<int>> mp;7
RandomizedCollection() {}12
mp[val].insert(arr.size() - 1);14
return mp[val].size() == 1;17
bool remove(int val) {19
// pick one occurance of val and delete it23
int idx = *(mp[val].begin());27
mp[val].erase(mp[val].begin());29
// replace this index with last element31
int last_val = arr.back();35
mp[last_val].insert(idx);37
mp[last_val].erase(arr.size() - 1);41
// if there is no occurance of val is present then erase it from map43
if (mp[val].size() == 0) {54
// generate the random index56
int rand_idx = rand() % arr.size();