集合
wjs
# 代码实现
class Set {
constructor() {
this.items = {};
}
add(value) {
if (this.has(value)) {
return false;
}
this.items[value] = value;
return true;
}
has(value) {
return this.items.hasOwnProperty(value)
}
remove(value) {
if (!this.has(value)) {
return false;
}
delete this.items[value];
return true;
}
size() {
return this.values().length;
}
clear() {
this.items = {};
}
values() {
return Object.keys(this.items)
}
// 并集 先将集合A中的所有元素添加到集合C中,再遍历集合B,如果是集合C所没有的元素就把它添加到集合C中。
union = otherSet => {
let unionSet = new Set();
let values = this.values();
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i])
}
values = otherSet.values();
for (let i = 0; i < values.length; i++) {
unionSet.add(values[i])
}
return unionSet;
}
// 交集 遍历集合A,当取得的元素也存在于集合B时,就把该元素添加到另一个集合C中。
intersection = otherSet => {
let intersectionSet = new Set();
let values = this.values();
for (let i = 0; i < values.length; i++) {
let item = values[i];
if (otherSet.has(item)) {
intersectionSet.add(item);
}
}
return intersectionSet;
}
// 差集 遍历集合A,当取得的元素不存在于集合B时,就把该元素添加到另一个集合C中。
difference = otherSet => {
let differenceSet = new Set();
let values = this.values();
for (let i = 0; i < values.length; i++) {
let item = values[i];
if (!otherSet.has(item)) {
differenceSet.add(item)
}
}
return differenceSet;
}
// 子集 遍历集合A,当取得的元素中有一个不存在于集合B时,就说明集合A不是集合B的子集,返回false。
subset = otherSet => {
let values = this.values()
for (let i = 0; i < values.length; i++) {
let item = values[i]
if (!otherSet.has(item)) {
return false
}
}
return true
}
}
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
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