学习js数据结构与算法4—集合

集合

集合是由一组无序且唯一的项组成的

6.1 创建一个集合

    function Set() {
        var items = {};
        /* 
            add(value):向集合添加一个新的项
            removeremove(value):从集合中移除一个值
            has(value):如果值在集合中存在,返回true,否则false
            clear(): 移除集合里所有的项
            size():返回集合所包含元素的数量
            values():返回一个包含集合中所有值的数组
        */
    
        // has(value)方法
        this.has = function (value) {
            return items.hasOwnProperty(value);
        };
    
        // add(value)方法
        this.add = function (value) {
            if (!this.has(value)) {
                items[value] = value;
                return true;
            }
            return false;
        };
        // remove(value)方法
        this.remove = function (value) {
            if (this.has(value)) {
                delete items[value];
                return true;
            }
            return false;
        };
        // clear()方法
        this.clear = function () {
            items = {};
        };
        // size()方法
        this.size = function () {
            return Object.keys(items).length;
        };
        // values()方法
        this.values = function () {
            return Object.keys(items);
        };
        
    }
    
    // 使用Set类
    var set = new Set();
    set.add(1);
    console.log(set.values());
    console.log(set.has(1));
    console.log(set.size());
    set.add(2);
    console.log(set.values());
    console.log(set.has(2));
    console.log(set.size());
    set.remove(1);
    console.log(set.values());
    set.remove(2);
    console.log(set.values());

6.2 集合操作

并集,交集,差集,子集

    // 并集
    this.union = function (other) {
        var union = new Set();

        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            union.add(values[i]);
        }

        values = other.values();
        for (var i = 0; i < values.length; i++) {
            union.add(values[i]);
        }

        return union;
    };

    // 交集
    this.intersection = function (other) {
        var section = new Set();

        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (other.has(values[i])) {
                section.add(values[i]);
            }
        }

        return section;
    };

    // 差集
    this.difference = function (other) {
        var difference = new Set();

        var values = this.values();
        for (var i = 0; i < values.length; i++) {
            if (!other.has(values[i])) {
                difference.add(values[i]);
            }
        }
        return difference;
    };

    // 子集
    this.subset = function (other) {

        if (this.size() > other.size()) {
            return false;
        } else {
            var values = this.values();
            for (var i = 0; i < values.length; i++) {
                if (!other.has(values[i])) {
                    return false;
                }
            }

            return true;
        }
    };
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容