 All Problems
Insert Delete GetRandom O(1)
medium
design
array
hash table
math
amazon
google
facebook

Implement the RandomizedSet class:

  • bool insert(int val) — insert val if not present; return true if inserted.
  • bool remove(int val) — remove val if present; return true if removed.
  • int getRandom() — return a random element with equal probability.

All functions must run in average O(1).

Example:

insert(1)→true, remove(2)→false, insert(2)→true, getRandom()→1or2,
remove(1)→true, insert(2)→false, getRandom()→2
Run to check your code against the sample cases, or submit to run every case