集合数组的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
// 集合
// 集合结构
// 哈希表
// 集合通常是一组无序的不能重复的元素构成,和数学中的集合名词比较相似,但是数学中的集合范围更大一些,也允许集合中的元素重复
// 在计算机中,集合通常表示的结构中元素是不允许重复的
// 把集合看成是特殊的数组
// 特殊之处在于里面的元素没有顺序,也不能重复
// 没有顺序意味着不能通过下标值进行访问,不能重复意味着相同的对象在集合中只会存在一份
// 封装集合类
// 封装集合的构造函数
function Set(){
    // 使用一个对象来保存集合的元素
    // 属性
    this.items = {}
    //集合的操作方法
    // add方法
    Set.prototype.add = function(value){
        //判断当前集合中是否已经包含了该元素
        if(this.has(value)){
            return false 
        }
        // 将元素添加到集合中
        this.items[value] = value 
        return true 
    }
    // has方法
    Set.prototype.has = function(value){
        return this.items.hasOwnProperty(value)
    }
    // remove方法
    Set.prototype.remove = function(value){
        if(!this.has(value)){
            return false 
        }
        delete this.items[value]
        return true 
    }
    Set.prototype.clear = function(){
        this.items = {}
    }
    // size方法
    Set.prototype.size = function(){
        return Object.keys(this.items).length
    }
    // 获取集合中所有的值
    Set.prototype.values = function(){
        return Object.keys(this.items)
    }
    // 集合间的操作
    // 并集
    Set.prototype.union = function(otherSet){
        // this集合和Other集合的合并
        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
    }
    // 交集
    Set.prototype.intersection = function(otherSet){
        // this集合A 
        // otherSet集合B
        var intersectionSet = new Set()
        // 从A中取出一个个元素
        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
    }
    // 差集
    Set.prototype.difference = function(otherSet){
        // this集合A
        // otherSet 集合B
        // 1.创建新的集合
        var differenceSet = new Set()
        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
    }
    // 子集
    Set.prototype.subset = function(otherSet){
        //this 集合A
        // otherSet集合B
        var values = this.values()
        for(var i=0;i<values.length;i++){
            var item = values[i]
            if(!otherSet.has(item)){
                return false 
            }
        }
        return true 

    }



}
// 测试set类
// var set = new Set()
// alert(set.add('abc'))
// alert(set.add('cbv'))
// alert(set.values())
// alert(set.remove('abc'))
// alert(set.remove('abc'))
// alert(set.values())
// alert(set.has('cbv'))
// alert(set.size())
// set.clear()
// alert(set.size())

// 集合间操作

// 集合间通常有如下操作:
// 并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合
// 交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合
// 差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合
// 子集:验证一个给定集合是否是另一个集合的子集
// 集合中并集的实现
// 1.创建两个集合,并且添加元素
var setA = new Set() 
setA.add('abc')
setA.add('ccc')
var setB = new Set()
setB.add('ccc')
setB.add('aaa')
unionSet = setA.union(setB)
alert(unionSet.values())
// 2.求两个集合的交集
var intersectionSet = setA.intersection(setB)
alert(intersectionSet.values())
// 3.差集 
var differenceSet = setA.difference(setB)
alert(differenceSet.values())
// 4.子集 
alert(setA.subset(setB))

    </script>
</body>
</html>
原文地址:https://www.cnblogs.com/smart-girl/p/13518123.html