Is there any similar implementation of ‘unordered_set’ / ‘unordered_map’ from c++, in javascript?
C++ Implementation
void someFunction(vector<int> &arr, int k){
unordered_set<int> s(arr.begin(), arr.end());
auto it = s.find(k); // TC: O(1)
if(it != s.end())
s.erase(k); // TC: O(1)
else
s.insert(k); // TC: O(1)
}
JS alternative try…
function someFunction(arr, k){
let s = new set(arr);
if(s.has(k)) // TC = ?
s.delete(k); // TC = ?
else
s.add(k); // TC = ?
}
2
Answers
You are close but you made it
set
where it should have beenSet
. SeeSet
@ Mozilla.TLDR: The javascript object type is effectively a map.
Also of note – Arrays are objects too in Javascript, So the array:
[33,44,55,66]
is mostly equivalent to: