You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
🔥 3 Approaches 🔥 || Simple Fast and Easy || with Explanation
Solution - 1 HashMap
import'dart:collection';
import'dart:math';
classRandomizedSet {
// Hashmap to keep track of thee value and the indexHashMap<int, int> valueToPosition =HashMap();
// whole list to store our valuesList<int> values = [];
// random number GeneratorRandom random =Random();
RandomizedSet() {
// initializationthis.random = random;
this.valueToPosition = valueToPosition;
this.values = values;
}
boolinsert(int val) {
// if our map have the key but no value than return falseif (valueToPosition.containsKey(val)) returnfalse;
// if so than we will store the value to our list
values.add(val);
// left side val as key and right side is index to keep track of the value
valueToPosition[val] = values.length -1;
// than return true because we have valuesreturntrue;
}
boolremove(int val) {
// if the key at position does not exist than return falseif (!valueToPosition.containsKey(val)) returnfalse;
// finding a position to remove the inserted valueint position = valueToPosition.remove(val) ??0;
// removing a the last index positionint last = values.removeAt(values.length -1);
// if the position and the length matches we will return trueif (position == values.length) returntrue;
// assigning the position of the element in ou list to the last element where we would remove
values[position] = last;
// setting the new value to our map to keep track of everything
valueToPosition[last] = position;
returntrue;
}
intgetRandom() {
// storing the whatever the number we will get from this functionreturn values[random.nextInt(values.length)];
}
}
Solution - 2 HashSet
import'dart:collection';
import'dart:math';
classRandomizedSet {
HashSet<int> hashSet =HashSet(); // since values given is in 1s and 2s//so not get confused between index i choose to store in form of stringsList<int> list = [];
RandomizedSet() {
this.hashSet = hashSet;
this.list = list;
}
boolinsert(int val) {
if (hashSet.contains(val)) returnfalse;
hashSet.add(val);
list.add(val);
returntrue;
}
boolremove(int val) {
if (hashSet.contains(val)) {
list.remove(val);
hashSet.remove(val);
returntrue;
}
returnfalse;
}
intgetRandom() {
return list[Random().nextInt(hashSet.length)];
}
}
Solution - 3 Array
import'dart:math';
classRandomizedSet {
List<int> list = [];
int size =0;
RandomizedSet() {
this.list = list;
this.size = size;
}
boolinsert(int val) {
int i = size;
while (i >0) {
if (val == list[--i]) returnfalse;
}
// size = size + 1;
list[size +1] = val;
if (size >= list.length) {
List<int> newSet = [2* size];
for (int j =0; j < size; j++) newSet[j] = list[j];
list = newSet;
}
returntrue;
}
boolremove(int val) {
int i = size -1;
while (i >=0) {
if (list[i] == val) break;
i--;
}
if (i ==-1) returnfalse;
swap(i, --size);
returntrue;
}
voidswap(int i, int j) {
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
intgetRandom() {
return list[Random().nextInt(size)];
}
}