Design a data structure that supports the following operations in average time complexity O(1)
:
insert(val)
: Inserts the element val
into the collection if it does not already exist.remove(val)
: Removes the element val
from the collection if it exists.getRandom
: Returns a random element from the existing collection, with each element having an equal probability of being returned.The problem requires implementing a data structure where insertion and deletion operations have a time complexity of O(1)
. It is easy to think of linked lists and hash tables. However, the problem also requires the time complexity for returning a random value to be O(1)
. Simple linked lists and hash tables cannot satisfy this requirement. Additionally, the time complexity of finding a value in a linked list given a specific value is O(n)
, which is not suitable for this problem. Therefore, to meet these requirements, we need to use a hash table in combination with an array. In this approach, the value serves as the key in the hash table, and the index in the array serves as the value in the hash table. This ensures that the time complexity for insert
and getRandom
operations is O(1
. For the remove
operation, the given value's index in the array is retrieved, and then the last value in the array is overwritten onto this index. Subsequently, the index of the last value in the hash table is updated, and finally, the last value in the array and the key representing the given value in the hash table are deleted. This implementation achieves an O(1)
time complexity for the remove
operation.
To begin, the constructor defines the object as the hash table and the array. In the insert
operation, if the value already exists in the hash table, it directly returns false
; otherwise, the value is added to the hash table as a key
, and the length of the array serves as the value
. The value is then appended to the array, and true
is returned. In the remove
operation, it first checks if the value does not exist and directly returns false
. If the value exists, it retrieves the index of the value and then retrieves the last value from the array. Subsequently, in the hash table, the key for this last value is replaced with the index, effectively overwriting the value to be deleted with the last value. Next, it deletes the index of the value to be removed from the hash table, replaces the value at the given index in the array with the last value, deletes the last value from the array, and finally, returns a random value from the array in the getRandom
operation.