/** Initialize your data structure here. */ RandomizedCollection() {
} /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ boolinsert(int val){ nums.push_back(val); idx[val].insert(nums.size() - 1); return idx[val].size() == 1; } /** Removes a value from the collection. Returns true if the collection contained the specified element. */ boolremove(int val){ if (idx.find(val) == idx.end()) { returnfalse; } int i = *(idx[val].begin()); nums[i] = nums.back(); idx[val].erase(i); idx[nums[i]].erase(nums.size() - 1); if (i < nums.size() - 1) { idx[nums[i]].insert(i); } if (idx[val].size() == 0) { idx.erase(val); } nums.pop_back(); returntrue; } /** Get a random element from the collection. */ intgetRandom(){ return nums[rand() % nums.size()]; } };
/** Initialize your data structure here. */ publicRandomizedCollection() { idx = newHashMap<Integer, Set<Integer>>(); nums = newArrayList<Integer>(); } /** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ publicbooleaninsert(int val) { nums.add(val); Set<Integer> set = idx.getOrDefault(val, newHashSet<Integer>()); set.add(nums.size() - 1); idx.put(val, set); return set.size() == 1; } /** Removes a value from the collection. Returns true if the collection contained the specified element. */ publicbooleanremove(int val) { if (!idx.containsKey(val)) { returnfalse; } Iterator<Integer> it = idx.get(val).iterator(); inti= it.next(); intlastNum= nums.get(nums.size() - 1); nums.set(i, lastNum); idx.get(val).remove(i); idx.get(lastNum).remove(nums.size() - 1); if (i < nums.size() - 1) { idx.get(lastNum).add(i); } if (idx.get(val).size() == 0) { idx.remove(val); } nums.remove(nums.size() - 1); returntrue; } /** Get a random element from the collection. */ publicintgetRandom() { return nums.get((int) (Math.random() * nums.size())); } }
/** * Initialize your data structure here. */ varRandomizedCollection = function() { this.idx = newMap(); this.nums = []; };
/** * Inserts a value to the collection. Returns true if the collection did not already contain the specified element. * @param {number} val * @return {boolean} */ RandomizedCollection.prototype.insert = function(val) { this.nums.push(val); const set = this.idx.has(val) ? this.idx.get(val) : newSet(); set.add(this.nums.length - 1); this.idx.set(val, set); return set.size === 1; };
/** * Removes a value from the collection. Returns true if the collection contained the specified element. * @param {number} val * @return {boolean} */ RandomizedCollection.prototype.remove = function(val) { if (!this.idx.has(val)) { returnfalse; } let i = undefined; for (const it ofthis.idx.get(val)) { if (!i) { i = it; break; } } const lastNum = this.nums[this.nums.length - 1]; this.nums[i] = lastNum; this.idx.get(val).delete(i); this.idx.get(lastNum).delete(this.nums.length - 1); if (i < this.nums.length - 1) { this.idx.get(lastNum).add(i); } if (this.idx.get(val).size === 0) { this.idx.delete(val); } this.nums.pop(); returntrue; };
/** * Get a random element from the collection. * @return {number} */ RandomizedCollection.prototype.getRandom = function() { returnthis.nums[Math.floor(Math.random() * this.nums.length)]; };
type RandomizedCollection struct { idx map[int]map[int]struct{} nums []int }
/** Initialize your data structure here. */ funcConstructor() RandomizedCollection { return RandomizedCollection{ idx: map[int]map[int]struct{}{}, } }
/** Inserts a value to the collection. Returns true if the collection did not already contain the specified element. */ func(r *RandomizedCollection) Insert(val int) bool { ids, has := r.idx[val] if !has { ids = map[int]struct{}{} r.idx[val] = ids } ids[len(r.nums)] = struct{}{} r.nums = append(r.nums, val) return !has }
/** Removes a value from the collection. Returns true if the collection contained the specified element. */ func(r *RandomizedCollection) Remove(val int) bool { ids, has := r.idx[val] if !has { returnfalse } var i int for id := range ids { i = id break } n := len(r.nums) r.nums[i] = r.nums[n-1] delete(ids, i) delete(r.idx[r.nums[i]], n-1) if i < n-1 { r.idx[r.nums[i]][i] = struct{}{} } iflen(ids) == 0 { delete(r.idx, val) } r.nums = r.nums[:n-1] returntrue }
/** Get a random element from the collection. */ func(r *RandomizedCollection) GetRandom() int { return r.nums[rand.Intn(len(r.nums))] }