-
Notifications
You must be signed in to change notification settings - Fork 0
/
Set.js
88 lines (86 loc) · 2.22 KB
/
Set.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
function mySet(){
var collection = [];
this.has = function(element){
return (collection.indexOf(element) !== -1)
}
this.values = function(){
return collection
}
this.add = function(element){
if(!this.has(element)){
collection.push(element);
return true
}
return false
}
this.remove = function(element){
if(this.has(element)){
index = collection.indexOf(element)
collection.splice(index,1)
return true
}
return false
}
this.size = function(){
return collection.length
}
this.union = function(otherSet){
var unionSet = new mySet()
var firstSet = this.values()
var secondSet = otherSet.values()
firstSet.forEach(function(e){
unionSet.add(e)
})
secondSet.forEach(function(e){
unionSet.add(e)
})
return unionSet
}
this.intersection = function(otherSet){
var intersectionSet = new mySet()
var firstSet = this.values()
firstSet.forEach(function(e){
if(otherSet.has(e)){
intersectionSet.add(e)
}
})
return intersectionSet
}
this.difference = function(otherSet){
var differenceSet = new mySet()
var firstSet = this.values()
firstSet.forEach(function(e){
if(!otherSet.has(e)){
differenceSet.add(e)
}
})
return differenceSet
}
this.subset = function(otherSet){
var firstSet = this.values()
return firstSet.every(function(value){
return otherSet.has(value)
})
}
}
var setA = new mySet()
var setB = new mySet()
setA.add("a")
setB.add("b")
setB.add("c")
setB.add("a")
setB.add("d")
console.log(setA.subset(setB))
console.log(setA.intersection(setB).values())
console.log(setB.difference(setA).values())
var setC = new Set()
var setD = new Set()
setC.add("a")
setD.add("b")
setD.add("c")
setD.add("a")
setD.add("d")
console.log(setD.values())
setD.delete("a")
console.log(setD.has("a"))
console.log(setD.add("d"))