I need a data structure for adding items, removing items, and picking a random item.
From what I've read, I think picking a random item from a list with random.choice
takes constant time. However, removing an item from a list takes O(n) time.
On the other hand, removing and adding an item from/to to a set take O(1) time, but picking a random element from a set with random.sample
takes O(n) time.
Is there a way to support those three operations in O(1) time?