-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSet.js
48 lines (42 loc) · 1.08 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
// Set Class
function Set() {
let collection = [];
this.has = function (element) {
return collection.indexOf(element) !== -1;
};
this.values = function () {
return collection;
};
this.add = function (element) {
if (this.has(element) === false) {
collection.push(element);
return true;
}
return false;
};
this.remove = function (element) {
if (this.has(element)) {
collection.splice(collection.indexOf(element), 1);
return true;
}
return false;
};
this.size = function () {
return collection.length;
};
this.union = function (arr) {
arr = arr.values();
collection = collection.concat(
arr.filter(function (item, pos) {
return collection.indexOf(item) == -1;
})
);
return this;
};
this.difference = function () {
arr = arr.values();
return arr.filter((item) => {
return collection.indexOf(item) === -1;
});
};
}