Set with O(1) removal and indexing
Write a class IndexedSet: a set of integers that keeps its values in slots numbered 0 to size - 1, so picking a uniformly random member would be just at(random index). Every operation must run in O(1) on average.
insert(x): ifxis absent, store it in the next free slot (indexsize) and returnTrue; otherwise returnFalse.remove(x): ifxis present, delete it, move the value from the last slot into the slot it left, and returnTrue; otherwise returnFalse.contains(x)returns whetherxis stored.at(i)returns the value in sloti;iis always valid.size()returns how many values are stored.
The tests call the provided run_set(ops): it applies each operation in order and returns the list of results.
Examples
run_set([["insert",4],["insert",9],["insert",4],["contains",9],["size"],["at",0],["at",1]])→[true,true,false,true,2,4,9]run_set([["insert",1],["insert",2],["insert",3],["remove",1],["at",0],["at",1],["size"],["contains",1]])→[true,true,true,true,3,2,2,false]