概念
- 集合是由一组无序且唯一(即不能重复)的项组成的
- 该数据结构使用了与有限集合相同的数学概念,但应用在计算机科学的数据结构中
- 集合的存储是值 和 值对应
集合方法
- add(element):向集合添加一个新元素。
- delete(element):从集合移除一个元素。
- has(element):如果元素在集合中,返回true,否则返回false。
- clear():移除集合中的所有元素。
- size():返回集合所包含元素的数量。它与数组的length 属性类似。
- values():返回一个包含集合中所有值(元素)的数组。
实现
构造存储结构:对象{value: value} 值 和 值
class set {constructor() {this.items = {};}}
判断元素唯一性的方法
has(ele) {return Object.prototype.hasOwnProperty.call(this.items, ele);}
各种方法的实现
add(ele) {if (!this.has(ele)) {this.items[ele] = ele;return true;}return false;}delete(ele) {if (this.has(ele)) {delete this.items[ele];return true;}return false;}clear() {this.items = {};}size() {// return Object.keys(this.items).length // es6写法let cnt = 0;for (let key in this.items) {if (this.has(key)) {cnt++;}}return cnt;}values() {let values = [];for (let key in this.items) {if (this.has(key)) {values.push(this.items[key]);}}return values;}
交集,并集,差集,子集
- 并集:对于给定的两个集合,返回一个包含两个集合中所有元素的新集合。
- 交集:对于给定的两个集合,返回一个包含两个集合中共有元素的新集合。
- 差集:对于给定的两个集合,返回一个包含所有存在于第一个集合且不存在于第二个集合的元素的新集合。
- 子集:验证一个给定集合是否是另一集合的子集。
并集运算
union(otherSet) {let unionSet = new MySet(); // 存储最终的并集集合数据this.values().forEach(value => {unionSet.add(value);});otherSet.values().forEach(value => {unionSet.add(value);});return unionSet;}
交集运算
intersection(otherSet) {let intersection = new MySet();let values = this.values(); // 预存当前集合值数组// 判断哪个大哪个小let otherValues = otherSet.values();let bigSet = undefined;let smallSet = undefined;if (values.length > otherValues.length) {bigSet = values;smallSet = otherValues;} else {bigSet = otherValues;smallSet = values;}for (let i = 0; i < smallSet.length; i++) {// 直接判断可能导致性能损耗,应该判断小的那个集合if (bigSet.has(smallSet[i])) {intersection.add(smallSet[i]);}}}
差集运算

difference(otherSet) {// 需要深拷贝let differenceSet = this;let intersection = this.intersection(otherSet);// 删除交集元素intersection.values().forEach(value => {differenceSet.delete(value);});return differenceSet;}
子集运算

isSubsetOf(otherSet) {if (this.size() > otherSet.size()) {return false;}let isSubset = true;this.values().every(value => {if (!otherSet.has(value)) {isSubset = false;return false;}return true;});return isSubset;}
完整代码
class MySet {constructor() {this.items = {};}// 判断元素的唯一性has(ele) {return Object.prototype.hasOwnProperty.call(this.items, ele);}add(ele) {if (!this.has(ele)) {this.items[ele] = ele;return true;}return false;}delete(ele) {if (this.has(ele)) {delete this.items[ele];return true;}return false;}clear() {this.items = {};}size() {// return Object.keys(this.items).length // es6写法let cnt = 0;for (let key in this.items) {if (this.has(key)) {cnt++;}}return cnt;}values() {let values = [];for (let key in this.items) {if (this.has(key)) {values.push(this.items[key]);}}return values;}union(otherSet) {let unionSet = new MySet(); // 存储最终的并集集合数据this.values().forEach(value => {unionSet.add(value);});otherSet.values().forEach(value => {unionSet.add(value);});return unionSet;}intersection(otherSet) {let intersection = new MySet();let values = this.values(); // 预存当前集合值数组// 判断哪个大哪个小let otherValues = otherSet.values();let bigSet = undefined;let smallSet = undefined;if (values.length > otherValues.length) {bigSet = values;smallSet = otherValues;} else {bigSet = otherValues;smallSet = values;}for (let i = 0; i < smallSet.length; i++) {// 直接判断可能导致性能损耗,应该判断小的那个集合if (bigSet.has(smallSet[i])) {intersection.add(smallSet[i]);}}}difference(otherSet) {// 需要深拷贝let differenceSet = this;let intersection = this.intersection(otherSet);// 删除交集元素intersection.values().forEach(value => {differenceSet.delete(value);});return differenceSet;}isSubsetOf(otherSet) {if (this.size() > otherSet.size()) {return false;}let isSubset = true;this.values().every(value => {if (!otherSet.has(value)) {isSubset = false;return false;}return true;});return isSubset;}}
