Insert Delete GetRandom O(1)

MediumArray

Description

Implement the RandomizedSet class with insert, remove, and getRandom methods, all with average O(1) time complexity. Return the result as an array.

Examples

Input:["insert",1],["remove",2],["insert",2],["getRandom"],["remove",1]
Output:[true,false,true,2,true]
Explanation:

Operations on the set.

Input:input = [[1],[2]]
Output:[]
Explanation:

This test case has no returned values, so the result is [].

Input:[["insert",5],["insert",3],["getRandom"],["insert",5],["remove",3],["getRandom"],["remove",5]]
Output:[true,true,3,false,true,5,true]
Explanation:

The insert, remove, and getRandom operations return [true,true,3,false,true,5,true].

Constraints

  • -2³¹ ≤ val ≤ 2³¹ - 1
  • At most 2 * 10⁵ calls to insert, remove, and getRandom

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!