Design a data structure that supports insert, delete, search and getRandom in constant time
Design a data structure that supports the following operations in Θ(1) time.
- insert(x): Inserts an item x to the data structure if not already present.
- remove(x): Removes item x from the data structure if present.
- search(x): Searches an item x in the data structure.
- getRandom(): Returns a random element from current set of elements
We can use hashing to support first 3 operations in Θ(1) time. How to do the 4th operation? The idea is to use a resizable array (ArrayList in Java, vector in C) together with hashing. Resizable arrays support insert in Θ(1) amortized time complexity. To implement getRandom(), we can simply pick a random number from 0 to size-1 (size is the number of current elements) and return the element at that index. The hash map stores array values as keys and array indexes as values.
Following are detailed operations.
- Check if x is already present by doing a hash map lookup.
- If not present, then insert it at the end of the array.
- Add in the hash table also, x is added as key and last array index as the index.
- Check if x is present by doing a hash map lookup.
- If present, then find its index and remove it from a hash map.
- Swap the last element with this element in an array and remove the last element.
Swapping is done because the last element can be removed in O(1) time.
- Update index of the last element in a hash map.
- Generate a random number from 0 to last index.
- Return the array element at the randomly generated index.
- Do a lookup for x in hash map.
Below is the implementation of the data structure.
2 3 30
This article is contributed by Manish Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.