...
/DIY: Insert Delete GetRandom O(1) - Duplicates Allowed
DIY: Insert Delete GetRandom O(1) - Duplicates Allowed
Solve the interview question "Insert Delete GetRandom O(1) - Duplicates Allowed" in this lesson.
We'll cover the following...
We'll cover the following...
Problem statement
Implement a set data structure that allows duplicates and can perform the following operations:
- insert(data): This function should insert- datainto the set (if the set does not contain it already). It should return- falseif the- dataalready exists in the set. Otherwise, it should return- true.
- remove(data): If the- datais present, this function should remove- datafrom the set and return- true. If the- datadoes not exist in the set, the function should return- false.
- getRandom(): This function should return a random element from the set in constant time.
Note: Your implementation should aim for constant running time (on average) for each operation. ...