JS数据结构与算法知识点--->集合

此数据结构算法知识点系列笔记均是看coderwhy老师视频整理得出!!!

集合比较常见的实现方式是哈希表(后续学习)

集合的特点

  • 无序的不能重复的
  • 是特殊的数组,没有顺序不能通过下标值进行访问,不能重复以为这相同的对象在集合中只会存在一份
  • 类似于Set,集合的实现其实可以直接使用Set类

集合的常见操作方法

  • add(value):向集合添加一个新的项。
  • remove(value):从集合移除一个值。
  • has(value):如果值在集合中,返回true,否则返回false。
  • clear():移除集合中的所有项。
  • size():返回集合所包含元素的数量,与数组的length属性类似。
  • values():返回一个包含集合中所有值的数组。
  • 还有一些集合其他相关的操作,暂时用不太多,这里暂不封装.
     

集合类的封装

<script>
      // 封装集合类
      function Set() {
        // 由于集合是无序的不能重复复的,所以此时就先不要定义成数组
        this.items = {};

        // 方法
        // 1.add
        Set.prototype.add = function (value) {
          if (this.has(value)) {
            return false;
          } else {
            this.items[value] = value;
            return true;
          }
        };
        // 2.has
        Set.prototype.has = function (value) {
          return this.items.hasOwnProperty(value);
        };
        // 3.remove
        Set.prototype.remove = function (value) {
          // 根据元素本身值进行删除,因为集合是没有下标值的
          if (this.has(value)) {
            delete this.items[value];
            return true;
          } else {
            return false;
          }
        };
        // 4.clear
        Set.prototype.clear = function () {
          this.items = {};
        };
        // 5.size
        Set.prototype.size = function () {
          return Object.keys(this.items).length;
        };
        // 6.values 获取集合中所有的值
        Set.prototype.values = function () {
          return Object.keys(this.items);
        };
      }

      let set = new Set();
  
    </script>

集合类操作

 并集

        Set.prototype.union = function (otherSet) {
          // this 集合对象A
          // otherSet 集合对象B
          var unionSet = new Set();
          var values = this.values();
          for (var i = 0; i < values.length; i++) {
            unionSet.add(values[i]);
          }
          values = otherSet.values();
          for (var i = 0; i < values.length; i++) {
            unionSet.add(values[i]);
          }
          return unionSet;
        };

 交集

        Set.prototype.intersection = function (otherSet) {
          var intersectionSet = new Set();
          var values = this.values();
		  for (var i = 0; i < values.length; i++) {
            var item = values[i]
			if(otherSet.has(item)){
				intersectionSet.add(item)
			}
          }
        };

 差集

        // 差集
        Set.prototype.difference = function (otherSet) {
          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) {
          var values = this.values();
          for (var i = 0; i < values.length; i++) {
            var item = values[i];
            if (!otherSet.has(item)) {
              return false;
            }
          }
          return true;
        };

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BoZai_ya

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值