javascript集合的交,并,补,子集的操作实现

本文介绍如何从零开始用JavaScript实现Set数据结构,并演示了如何进行元素的添加、删除、查找等操作,还提供了并集、交集、差集及子集判断的实现方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可能新的ECMA规范里已有了这些的实现,

但能自己从头开始实现,感觉也非常不错的哟。。。

function  Set() {
    var items = {};

    this.has = function(value){
        return items.hasOwnProperty(value);
    };

    this.add = function(value){
        if (!this.has(value)){
            items[value] = value;
            return true;
        }
        return false;
    };

    this.remove = function(value){
        if (this.has(value)) {
            delete items[value];
            return true;
        }
        return false;
    };

    this.clear = function(){
        items = {};
    };

    this.size = function(){
        var count = 0;
        for (var prop in items){
            if(items.hasOwnProperty(prop)){
                ++count;
            }
        }
        return count;
    };

    this.values = function(){
        var keys = [];
        for (var key in items){
            keys.push(key);
        }
        return keys;
    };

    this.union = function(otherSet){
        var unionSet = new Set();

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

        var values = otherSet.values();
        for(var i=0; i<values.length; i++){
            unionSet.add(values[i]);
        }
        return unionSet;
    };

    this.intersection = function(otherSet){
        var intersectionSet = new Set();

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

    this.difference = function(otherSet){
        var differenceSet = new Set();

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

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

var set = new Set();
set.add(1);
console.log(set.values()); //输出["1"]
console.log(set.has(1)); //输出true
console.log(set.size()); //输出1
set.add(2);
console.log(set.values()); //输出["1", "2"]
console.log(set.has(2)); //true
console.log(set.size()); //2
set.remove(1);
console.log(set.values()); //输出["2"]
set.remove(2);
console.log(set.values()); //输出[]

var setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
var setB = new Set();
setB.add(3);
setB.add(4);
setB.add(5);
setB.add(6);
var unionAB = setA.union(setB);
console.log(unionAB.values());

var setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
var setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);
var intersectionAB = setA.intersection(setB);
console.log(intersectionAB.values());

var setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
var setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);
var differenceAB = setA.difference(setB);
console.log(differenceAB.values());

var setA = new Set();
setA.add(1);
setA.add(2);
var setB = new Set();
setB.add(1);
setB.add(2);
setB.add(3);
var setC = new Set();
setC.add(2);
setC.add(3);
setC.add(4);
console.log(setA.subset(setB));
console.log(setA.subset(setC));

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值