Design a data structure that supports the following operations in average time:
add(x) -> bool
x
into the collection.
true
if
x
was
not already present
in the collection before this call; otherwise returns
false
.
delete(x) -> bool
x
from the collection (if present).
true
if an occurrence was removed; otherwise returns
false
.
random() -> int
N
total elements counting duplicates,
random()
should pick
uniformly among the N occurrences
.
If the collection contains [5, 5, 7], then:
P(random() = 5) = 2/3
P(random() = 7) = 1/3