Insert Delete GetRandom O(1) Flashcards

1
Q

Insert Delete GetRandom O(1)

Design a data structure that supports all following operations in average O(1) time.

insert(val): Inserts an item val to the set if not already present.
remove(val): Removes an item val from the set if present.
getRandom: Returns a random element from current set of elements. Each element must have the same probability of being returned.

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly