-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSet.js
More file actions
75 lines (68 loc) · 1.67 KB
/
Set.js
File metadata and controls
75 lines (68 loc) · 1.67 KB
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
//참조자료 : https://www.youtube.com/watch?v=wl8u02IdVxo
//Implemetation of Set
// kind of an array except no duplicated item
function mySet() {
let 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) {
let unionSet = new mySet();
let firstSet = this.values();
let secondSet = otherSet.values();
firstSet.forEach(function (e) {
unionSet.add(e);
});
secondSet.forEach(function (e) {
unionSet.add(e);
});
return unionSet;
};
this.intersection = function (otherSet) {
let intersectionSet = new mySet();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (otherSet.has(e)) {
intersectionSet.add(e);
}
});
return intersectionSet;
};
this.difference = function (otherSet) {
let differenceSet = new mySet();
let firstSet = this.values();
firstSet.forEach(function (e) {
if (!otherSet.has(e)) {
differenceSet.add(e);
}
});
return differenceSet;
};
this.subset = function (otherSet) {
let firstSet = this.values();
return firstSet.every(function (value) {
return otherSet.has(value);
});
};
}