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 an 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.
1) Check if x is already present by doing a hash map lookup.
2) If not present, then insert it at the end of the array.
3) Add in the hash table also, x is added as key and last array index as the index.
1) Check if x is present by doing a hash map lookup.
2) If present, then find its index and remove it from a hash map.
3) 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.
4) Update index of the last element in a hash map.
1) Generate a random number from 0 to last index.
2) 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 40
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
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 stack that supports getMin() in O(1) time and O(1) extra space
- Queries to insert, delete one occurrence of a number and print the least and most frequent element
- Design and Implement Special Stack Data Structure | Added Space Optimized Version
- Design a data structure for LRU Cache
- Trie | (Insert and Search)
- Binary Search Tree | Set 2 (Delete)
- Binary Search Tree | Set 1 (Search and Insertion)
- Anagram Substring Search (Or Search for all permutations)
- Design data structures and algorithms for in-memory file system
- Design the Data Structures(classes and objects)for a generic deck of cards
- Connect nodes at same level using constant extra space
- Create a customized data structure which evaluates functions in O(1)
- Efficiently find first repeated character in a string without using any additional data structure in one traversal
- Remove duplicates from unsorted array using Map data structure
- Given a linked list which is sorted, how will you insert in sorted way
- Sorted insert for circular linked list
- Real-time application of Data Structures
- Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?
- Delete a node in a Doubly Linked List
- Delete N nodes after M nodes of a linked list