class RandomizedSet {
List<Integer> list;
Map<Integer,Integer> map;
public RandomizedSet() {
list = new ArrayList<>();
map = new HashMap<>();
}
public boolean insert(int val) {
if (map.containsKey(val)) return false;
list.add(val);
map.put(val,list.size() - 1);
return true;
}
public boolean remove(int val) {
if (!map.containsKey(val)) return false;
int curIndex = map.get(val);
int lastIndex = list.size() - 1;
map.put(list.get(lastIndex), curIndex);
list.set(curIndex,list.get(list.size() -1));
map.remove(val);
list.remove(lastIndex);
return true;
}
public int getRandom() {
int random = (int)(Math.random() * (list.size()));
return list.get(random);
}
}