Design a data structure that supports insert, delete, getRandom in O(1) with duplicates
- insert(x): Inserts x in the data structure. Returns True if x was not present and False if it was already present.
- remove(x): Removes x from the data structure, if present.
- getRandom(): Returns any value present in the stream randomly. The probability of each element being returned should be linearly proportional to the number of same valued elements the stream contains.
Approach: In the previous article, we have already discussed an approach for this kind of data structure. However, the previous data structure worked well only for unique values. In this article, we will design a data structure which can handle duplicate elements also.
The approach used in this article is much similar to the previous approach, but in order to handle the duplicate elements, a map of sets is used to store the indices of the elements present in the dynamic array. Let’s understand every method independently.
- insert(int x):
- Insert x at the end of the dynamic array nums.
- Insert the index of x (i.e.) nums.size() – 1 to mp[x]. This map of sets stores all the indices of the element x present in the dynamic array nums.
- remove(int x):
- Check if x is present in the stream by mp.count(x). If it is absent, then return False.
- If x is present, the first element of the set mp[x] is deleted and its value is stored in a variable indexRemoved. Now, if this element (i.e.) indexRemoved is the same as nums.length() – 1 go directly to step 6 because this means that the element is already at the last index and that element is deleted in constant time.
- If not, then in order to delete this element in constant time, this element is swapped with the last element in the dynamic array. Therefore, delete the value nums.size() – 1 from the mp[nums[nums.size() – 1]] set.
- Insert the value index into mp[nums[nums.size() – 1]] set.
- Swap the elements at index nums.size() – 1 and indexRemoved of nums.
- Delete the last element from nums (Removal from the end from Dynamic Array is constant time operation).
- If the mp[val] set becomes empty, erase val from mp.
- Return True
- Get a random number between 0 and nums.size() – 1.
- Return the value present at this index of nums.
Below is the implementation of the above approach:
1 1 0 1 0 5