- 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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Design a data structure that supports insert, delete, search and getRandom in constant time
- Efficiently design Insert, Delete and Median queries on a set
- Design an efficient data structure for given operations
- Design a Queue data structure to get minimum or maximum in O(1) time
- Treap | Set 2 (Implementation of Search, Insert and Delete)
- Data Structure for Dictionary and Spell Checker?
- Ropes Data Structure (Fast String Concatenation)
- Advantages of Trie Data Structure
- Tango Tree Data Structure
- Dynamic Disjoint Set Data Structure for large range values
- Gap Buffer Data Structure
- Trie Data Structure using smart pointer and OOP in C++
- Introduction to the Probabilistic Data Structure
- Inversion Count using Policy Based Data Structure
- Sorting array of strings (or words) using Trie | Set-2 (Handling Duplicates)
- Trie | (Insert and Search)
- Insert Operation in B-Tree
- Splay Tree | Set 2 (Insert)
- Red-Black Tree | Set 2 (Insert)
- K Dimensional Tree | Set 1 (Search and Insert)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.