(五)学习javascript数据结构与算法——Set集合

今天学习数据结构与算法中的集合

1.集合的特点:项无序且唯一

2.

(1)集合的基本骨架:

function Set(){
    let items=[];
}

(2)集合中的方法:

        this.has(value);//如果值在集合中,返回true,否则返回false
        this.add(value);//向集合添加一个新的项
        this.remove(value);//从集合移除一个值
        this.clear();//移除集合中的所有项
        this.size();//返回集合所包含元素的数量
        this.values();//返回一个包含集合所有值的数组

(3)具体的实现:

首先实现的是has方法。这个在add和remove方法中会使用到。由于使用对象来存储集合的值,可以通过in操作符来验证给定的值是否items对象的属性。

this.has=function(value){
    return value in itmes;
}

接着实现add方法:

先判断添加的值是否已经存在于集合中,如果存在,则返回false,表示不添加,如果不存在,则添加

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

实现remove方法:

同理在删除值前需要进行判断,如果存在,则通过delete删除,如果不存在,则返回false,表示不删除。

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

清空集合:

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

实现size方法:

this.size=function(){
    return Object.keys(items).length;
}

实现values方法:

this.values=function(){
    let val=[];
    for(let key in items){
        if(items.hasOwnProperty(key)){
            val.push(items[key]);
        }
    }
    return values;
}

测试Set类:

let set=new Set();
    set.add(1);
    set.add(2);
    console.log(set.values());
    console.log(set.has(1));
    console.log(set.size());
    set.remove(2);
    console.log(set.values());
    console.log(set.size());

实现结果截图:

(4)集合的操作:

并集、交集、差集、子集

union方法:

 //并集
        this.union=function(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;
        }

交集:

 //交集
        this.intersection=function(otherSet){
            let intersectionSet=new Set();
            let values=this.values();
            for(let i=0;i<valuse.length;i++){
                if(otherSet.has(values[i])){
                    intersectionSet.add(values[i]);
                }
            }
            return intersectionSet;
        }
 //差集
        this.difference=function(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;
        }
  //子集
        this.subset=function(otherSet){
            if(this.size()>otherSet.size()){
                return false;
            }else{
                let values=this.values();
                for(let i=0;i<values.length;i++){
                    if(!otherSet.has(values[i])){
                        return false;
                    }
                }
                return true;
            }
        }

暂时写到这里,之后再继续补充!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值