Js算法中的集合

158 阅读2分钟

1,集合的定义

2,并集

3,交集

4,差集

5,子集

5,集合的常见操作

  • add(value):向集合添加一个新的元素
  • remove(value):从集合移除一个值
  • has(value):如果在集合中,返回true,否则返回false
  • clear():移除集合所有的项
  • size():返回集合所包含的元素数量,与数组的length属性一样
  • values():返回一个包含所有集合的数组
  • union(otherSet):求两个集合的并集
  • intersection(otherSet):求两个集合的交集
  • difference(otherSet):求两个集合的差集
  • subset(otherSet):求两个集合是否属于子集关系

6,集合的实现

class Set{
    constructor(){
        this.items={}
    }
    //add方法
    add(value){
        //判断当前集合是否包含了该元素
        if(this.has(value)){
            return false
        }
        //将元素添加到集合中
        this.items[value]=value
        return true
    }
    //has方法
    has(value){
        return this.hasOwnProperty(value)
    }
    //remove方法
    remove(){
        //判断集合是否包含该元素
        if(!this.has(value)){
            return false
        }
        delete this.items[value]
        return true
    }
    //clear方法
    clear(){
        this.items={}
    }
    //size方法
    size(){
        return Object.keys(this.items)
    }
    //并集
    union(otherSet){
        //this:集合对象A
        //otherSet:集合对象B
        //创建新的集合
        var unionSet=new Set()
        //将集合A的所有元素添加到新的集合中
        var values=this.values()
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }
        //取出B集合中的元素,判断是否需要添加到新的集合
        values=otherSet.values()
        for(var i=0;i<values.length;i++){
            unionSet.add(values[i])
        }
        return unionSet
    }
    //交集
    insersection(otherSet){
        //this:表示集合A
        //otherSet:表示集合B
        //创建新的集合
        var intersectionSet=new Set()
        //从A中取出一个个的元素,判断是否同时存在集合B中,存在放入新的集合
        var values=this.values()
        for(var i=0;i<values.length;i++){
            var item=values[i]
            if(otherSet.has(item)){
                intersectionSet.add(item)
            }
        }
        return intersectionSet
    }
    //差集
    difference(otherSet){
        //this:集合A
        //otherSet:集合B
        //创建新的集合
        var differenceSet=new Set()
        //取出A集合的一个个元素,判断是否同时存在B集合中,不在B,则添加到新的集合
        var values=this.values()
        for(var i=0;i<values.length;i++){
            var item=values[i]
            if(!otherSet.has(item)){
                differenceSet.add(item)
            }
        }
        return differenceSet
    }
    //子集
    subset(otherSet){
        //this:集合A
        //otherSet:集合B
        //遍历A集合中的所有元素,如果发现集合A中的元素,在集合B中不存在,则返回false
        var values=this.values()
        for(var i=0;i<values.length;i++){
            var item=values[i]
            if(!otherSet.has(item)){
                return false
            }
        }
        return true
    }
}
var setA=new Set()
setA.add('one')
setA.add('two')
set.add('three')
var setB=new Set()
setB.add('one')
setB.add('two')
setB.add('four')
console.log(setA.union(setB).values(),"union")//求并集
console.log(setA.intersection(setB).values(),"intersection")//求交集
console.log(setA.difference(setB).values(),"difference")//求差集
console.log(setA.subset(setB),"subset")//求子集