Insert Delete GetRandom O(1) - Duplicates Allowed
Problem statement
Process commands on a multiset supporting average O(1) insertion, removal, and uniform random sampling over stored occurrences.
insert xadds one occurrence and returns true exactly when x was previously absent.remove xremoves one occurrence and returns whether removal occurred.getRandomreturns a uniformly random stored occurrence.
Return one string result per command. For deterministic judging, every getRandom command occurs when all stored occurrences have the same value.
Function
processRandomizedCollection(operations: String[]) → String[]Examples
Example 1
operations = ["insert 1","insert 1","insert 2","remove 1","remove 2","getRandom"]return = ["true","false","true","true","true","1"]One occurrence of 1 remains before getRandom.
Constraints
1 <= operations.length <= 10^4.- Every getRandom is issued on a nonempty collection whose distinct-value count is one.