function Set() {
this.items = {};
this.length = 0;
Set.prototype.add = function (value) {
if (this.has(value)) {
return false;
}
this.items[value] = value;
this.length += 1;
};
Set.prototype.remove = function (value) {
if (!this.has(value)) {
return false;
}
const { [value]: removeObj, ...otherObj } = this.items;
this.items = { ...otherObj };
this.length -= 1;
return true;
};
Set.prototype.has = function (value) {
return this.items.hasOwnProperty(value);
};
Set.prototype.clear = function () {
this.items = {};
this.length = 0;
};
Set.prototype.size = function () {
return this.length;
};
Set.prototype.values = function () {
return Object.values(this.items);
};
Set.prototype.union = function (newSet) {
const unionSet = new Set();
let values = this.values();
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
values = newSet.values();
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i]);
}
return unionSet;
};
}
const set = new Set();
set.add(22);
set.add(223);
set.add(224);
set.add(225);
const newSet = new Set();
newSet.add(22);
newSet.add(223);
newSet.add(222342);
newSet.add(34534543);
console.log("set.union(newSet);", set.union(newSet).values());