forked from ohxxx/algorithm-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.js
More file actions
58 lines (47 loc) · 987 Bytes
/
Copy pathset.js
File metadata and controls
58 lines (47 loc) · 987 Bytes
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
class Set {
constructor() {
this.items = {}
}
// 添加新元素
add(element) {
if (!this.has(element)) {
this.items[element] = element
return true
}
return false
}
// 移除元素
delete(element) {
if (this.has(element)) {
delete this.items[element]
return true
}
return false
}
// 判断元素是否存在集合中
has(element) {
return Object.prototype.hasOwnProperty.call(this.items, element)
}
// 移除集合中所有元素
clear() {
this.items = {}
}
// 返回集合所包含元素的数量
size() {
return Object.keys(this.items).length
}
// 返回一个包含集合中所有值(元素)的数组
values() {
return Object.values(this.items)
}
}
// test
const xxx = new Set()
xxx.add(111)
xxx.add(222)
xxx.add(333)
xxx.delete(333)
console.log('has', xxx.has(111));
console.log('size', xxx.size());
console.log('values', xxx.values());
console.log(xxx.items);