We can find unique elements of array using JavaScript Set()
. You can create a set by either giving array initially or use Set.prototype.add()
. What are time complexities and space complexities for each case?
With constructor
const mySet = new Set(nElementsArray);
With add() method
const mySet = new Set();
for (const item of nElementsArray) {
mySet.add(item);
}
For each of above what will be space and time complexity?