-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path集合.js
118 lines (111 loc) · 2.77 KB
/
集合.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
* @Descripttion:
* @version:
* @Author: wenlan
* @Date: 2022-03-23 21:34:24
* @LastEditors: wenlan
* @LastEditTime: 2022-03-23 22:12:26
*/
//手动封装集合
class Set {
constructor() {
this.items = {};
}
has(value) {
return this.items.hasOwnProperty(value);
}
//在集合中添加元素
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];
}
//清空
clear() {
this.items = {};
}
size() {
return Object.keys(this.items).length;
}
//获取集合中所有元素
values() {
return Object.keys(this.items);
}
//集合并集
union(otherSet) {
const unionSet = new Set();
//第一个集合元素
for (const value of this.values()) {
unionSet.add(value);
}
//第二个集合元素
for (const value of otherSet.values()) {
unionSet.add(value);
}
return unionSet;
}
//集合交集
intersection(otherSet) {
const unionSet = new Set();
//从当前集合取出values 判断另一个集合是否存在 则add新集合
for (const value of this.values()) {
if (otherSet.has(value)) {
unionSet.add(value);
}
}
return unionSet;
}
//差集
different(otherSet) {
const unionSet = new Set();
//从当前集合取出values 判断另一个集合是否存在 则add新集合
for (const value of this.values()) {
if (!otherSet.has(value)) {
unionSet.add(value);
}
}
return unionSet;
}
//子集
subset(otherSet) {
for (const value of this.values()) {
if (!otherSet.has(value)) {
return false;
}
return true;
}
}
}
const set = new Set();
// add() 测试
set.add("abc");
set.add("abc");
set.add("123");
set.add("zxc");
console.log(set); //--> {items: {123: "123", abc: "abc", zxc: "zxc"}}
const set1 = new Set();
// add() 测试
set1.add("abc");
set1.add("wenlan");
set1.add("yyds");
console.log(set1);
console.log(set.union(set1));
console.log(set.intersection(set1));
console.log(set.subset(set1));
// // has() 测试
// console.log(set.has("123")); //--> true
// console.log(set.has("456")); //--> false
// // remove() 测试
// set.remove("abc");
// console.log(set); //--> {items: {123: "123", zxc: "zxc"}}
// // size() 测试
// console.log(set.size()); //--> 2
// // values() 测试
// console.log(set.values()); //--> ["123", "zxc"]
// // clear() 测试
// // set.clear();
// console.log(set.values()); //--> []