美文网首页
JavaScript 数据结构之集合

JavaScript 数据结构之集合

作者: 前端程序猿 | 来源:发表于2020-12-13 18:13 被阅读0次

一、 集合的介绍

几乎每一种编程语言中,都有集合结构, 常见是实现方式是哈希表,这里我们使用 JavaScript 中的 Object 实现集合

集合: 由一组无序的,不重复的元素构成

ES6 中已经实现了 Set 类, 可以直接使用,但为了明确集合内部的实现机制,这里还是封装一下 Set 类

二、 集合的基本操作

class Set {
  constructor() {
    this.items = {};
  }

  // 添加新元素
  add(value) {
    if (this.has(value)) {
      return false;
    }

    this.items[value] = value;
    return true;
  }

  // 删除元素
  remove(value) {
    if (!this.has(value)) {
      return false;
    }
    delete this.items[value];
    return true;
  }

  // 检测是否包含特定值
  has(value) {
    return this.items.hasOwnProperty(value);
  }

  // 清空集合
  clear() {
    this.items = {};
  }

  // 返回集合元素个数
  size() {
    return Object.keys(this.items).length;
  }

  // 返回集合值组成的数组
  values() {
    return Object.keys(this.items);
  }
}

三、 并集

// 并集
union (otherSet) {
  const union = new Set()
  const values = this.values()
  const otherValues = otherSet.values()
  for (const value of values) {
    union.add(value)
  }
  for (const value of otherValues) {
    union.add(value)
  }
  return union
}

四、 差集

// 差集
difference (otherSet) {
  const difference = new Set()
  const values = this.values()
  for (const value of values) {
    if (!otherSet.has(value)) {
      difference.add(value)
    }
  }
  return difference
}

五、 交集

// 交集
intersection (otherSet) {
  const intersection = new Set()
  const values = this.values()
  for (const value of values) {
    if (otherSet.has(value)) {
      intersection.add(value)
    }
  }
  return intersection
}

六、 子集

// 子集
isSubSetOf (otherSet) {
  const values = this.values()
  for (const value of values) {
    if (!otherSet.has(value)) {
      return false
    }
  }
  return true
}

七、 完整代码

class Set {
  constructor() {
    this.items = {};
  }

  // 添加新元素
  add(value) {
    if (this.has(value)) {
      return false;
    }

    this.items[value] = value;
    return true;
  }

  // 删除元素
  remove(value) {
    if (!this.has(value)) {
      return false;
    }
    delete this.items[value];
    return true;
  }

  // 检测是否包含特定值
  has(value) {
    return this.items.hasOwnProperty(value);
  }

  // 清空集合
  clear() {
    this.items = {};
  }

  // 返回集合元素个数
  size() {
    return Object.keys(this.items).length;
  }

  // 返回集合值组成的数组
  values() {
    return Object.keys(this.items);
  }

  // 并集
  union(otherSet) {
    const union = new Set();
    const values = this.values();
    const otherValues = otherSet.values();
    for (const value of values) {
      union.add(value);
    }
    for (const value of otherValues) {
      union.add(value);
    }
    return union;
  }

  // 交集
  intersection(otherSet) {
    const intersection = new Set();
    const values = this.values();
    for (const value of values) {
      if (otherSet.has(value)) {
        intersection.add(value);
      }
    }
    return intersection;
  }

  // 差集
  difference(otherSet) {
    const difference = new Set();
    const values = this.values();
    for (const value of values) {
      if (!otherSet.has(value)) {
        difference.add(value);
      }
    }
    return difference;
  }

  // 子集
  isSubSetOf(otherSet) {
    const values = this.values();
    for (const value of values) {
      if (!otherSet.has(value)) {
        return false;
      }
    }
    return true;
  }
}

相关文章

  • JavaScript 数据结构之集合

    一、 集合的介绍 几乎每一种编程语言中,都有集合结构, 常见是实现方式是哈希表,这里我们使用 JavaScript...

  • js数据结构之栈

    JavaScript数据结构 一、什么是数据结构? 数据结构是向相互之间存在一种或者多种特定关系的数据组成的集合,...

  • 学习javascript数据结构(三)——集合

    前言 总括: 本文讲解了数据结构中的[集合]概念,并使用javascript实现了集合。 原文博客地址:学习jav...

  • 22- Iterator 遍历器

    1、什么是 Iterator遍历器? JavaScript 原有的表示“集合”的数据结构,主要是数组(Array)...

  • ES6 Iterator和for...of 循环

    Iterator概念 JavaScript原有的表示“集合”的数据结构,主要是数组(Array)和对象(Objec...

  • javaScript原型链

    javaScript原型链概念JavaScript之继承(原型链)数据结构var Person = functio...

  • javaScript数据结构--集合

    集合是由一组无序且唯一的项组成的: 集合可以进行 并集、交集、差集、子集操作。 集合的代码实现: function...

  • 前端常见数据结构小结

    常见数据结构的 JavaScript 实现 栈 队列 链表 集合 字典 哈希表 二叉树 图 前端与数据结构 数据结...

  • JS中数据结构的遍历--Iterator和for...of循环

    JavaScript原有的四种表示'集合'的数据结构,Object、Array、Set、Map。 遍历器(Iter...

  • Iterator(遍历器)和for..of

    概念## JavaScript原有的表示“集合”的数据结构,主要是数组(Array)和对象(Object),ES6...

网友评论

      本文标题:JavaScript 数据结构之集合

      本文链接:https://www.haomeiwen.com/subject/lnspgktx.html