JavaScript手动实现集合Set类

class Set {
    constructor() {
        this.items = {};
    }
    add(element) {
        if (this.has(element)) {
            return false;
        } else {
            this.items[element] = element;
            return true;
        }
    }
    delete(element) {
        if (this.has(element)) {
            delete this.items[element];
            return true;
        } else {
            return false;
        }

    }
    has(element) {
        return Object.prototype.hasOwnProperty.call(this.items, element);
    }
    size() {
        return Object.keys(this.items).length;
    }
    /*  手动实现size()
    size() {
        let count = 0;
        for (key in this.items) {
            if (this.has(key)) {
                count++;
            }
        }
        return count;
    } */
    values() {
        return Object.values(this.items);
    }
    /*    手动实现values
       values() {
           let valuesArray = [];
           for (key in this.items) {
               if (this.has(key)) {
                   valuesArray.push(key);
               }
           }
           return valuesArray;
       } */
    clear() {
        this.items = {};
    }
    isEmpty() {
        return this.size() === 0;
    }
    //并集
    union(otherSet) {
        let unionSet = new Set();
        this.values().forEach(element => {
            unionSet.add(element);
        });
        otherSet.values().forEach(element => {
            unionSet.add(element);
        });
        return unionSet;
    }
    //交集
    intersection(otherSet) {
        let intersectionSet = new Set();
        let biggerValues;
        let smallerValues;
        if (this.values().length > otherSet.values().length) {
            biggerValues = this.values();
            smallerValues = otherSet.values();
        } else {
            biggerValues = otherSet.values();
            smallerValues = this.values();
        }
        smallerValues.forEach(x => {
            if (biggerValues.includes(x)) {
                intersectionSet.add(x);
            }
        });
        return intersectionSet;
    }
    //差集
    difference(otherSet) {
        let differenceSet = new Set();
        this.values().forEach(x => {
            if (!otherSet.has(x)) {
                differenceSet.add(x);
            }
        });
        return differenceSet; s
    }
    //子集
    isSubsetOf(otherSet) {
        if (this.size() > otherSet.size()) {
            return false;
        }
        if (this.size() === 0) {   //空集是任何集合的子集
            return true;
        }

        return this.values().every(x => {   //every不会对空数组进行检测
            if (!otherSet.has(x)) {
                return false;
            } else {
                return true;
            }

        });
    }
}


//测认用例
const set = new Set();

set.add(1);
console.log(set.values()); // outputs [1]
console.log(set.has(1)); // outputs true
console.log(set.size()); // outputs 1

set.add(2);
console.log(set.values()); // outputs [1, 2]
console.log(set.has(2)); // true
console.log(set.size()); // 2

set.delete(1);
console.log(set.values()); // outputs [2]

set.delete(2);
console.log(set.values()); // outputs []

// --------- Union ----------

let setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);

let setB = new Set();
setB.add(3);
setB.add(4);
setB.add(5);
setB.add(6);

const unionAB = setA.union(setB);
console.log(unionAB.values()); // [1, 2, 3, 4, 5, 6]

// --------- Intersection ----------

setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);

setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);

const intersectionAB = setA.intersection(setB);
console.log(intersectionAB.values()); // [2, 3]

// --------- Difference ----------

setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);

setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);

const differenceAB = setA.difference(setB);
console.log(differenceAB.values()); // [1]

// --------- Subset ----------

setA = new Set();
setA.add(1);
setA.add(2);

setB = new Set();
setB.add(1);
setB.add(2);
setB.add(3);

const setC = new Set();
setC.add(2);
setC.add(3);
setC.add(4);

console.log(setA.isSubsetOf(setB)); // true
console.log(setA.isSubsetOf(setC)); // false

 

posted @ 2020-11-03 22:31  WP-WangPin  阅读(171)  评论(0编辑  收藏  举报