Set
集合
by Chris_Walter
JavaScript
class Set{
constructor(){
this.items = {};
}
has(value){
return this.items.hasOwnProperty(value);
}
add(value){
//如果這個值不存在才新增至物件,存在則直接回傳false
if(!this.has(value)){
this.items[value] = value;
return true;
}
return false;
}
remove(value){
if(this.has(value)){
delete this.items[value];
return true;
}
return false;
}
clear(){
this.items = {};
}
size(){
return Object.keys(this.items).length;//只能在現代瀏覽器中運行
}
/*
sizeLegacy(){
let count = 0;
for(let num in this.items){
if(this.has(num)){
++count;
}
return count;
}
}
*/
values(){
return Object.keys(this.items);
}
/*
valuesLegacy(){
let keys = [];
for(let key in this.items){
keys.push(key);
}
return keys;
}
*/
//聯集
union(otherSet){
let unionSet = new Set();
let values = this.values();
for(let i=0; i<values.length; i++){
unionSet.add(values[i]);
}
values = otherSet.values();
for(let i=0; i<values.length; i++){
unionSet.add(values[i]);
}
return unionSet
}
//交集
intersection(otherSet){
let intersectionSet = new Set();
let values = this.values();
for(let i=0; i<values.length; i++){
if(otherSet.has(values[i])){
intersectionSet.add(values[i]);
}
}
return intersectionSet;
}
//差集
difference(otherSet){
let differenceSet = new Set();
let values = this.values();
for(let i=0; i<values.length; i++){
if(!otherSet.has(values[i])){
differenceSet.add(values[i]);
}
}
return differenceSet;
}
//子集
subSet(otherSet){
let values = this.values();
if(this.size() > otherSet.size()){
return false;
} else {
for(let i=0; i<values.length; i++){
if(!otherSet.has(values[i])){
return false;
}
}
return true;
}
}
}
let n1 = new Set();
n1.add('Alan');
console.log(n1);
n1.remove('Alan');
console.log(n1);
console.log(n1.size());
n1.add('John');
console.log(`size: ${n1.size()}`);
//console.log(`sizeLegacy: ${n1.sizeLegacy()}`);
let nA...