-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday18.js
76 lines (60 loc) · 1.6 KB
/
day18.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
//ser operations
// perform union operation
// contain elements of both sets
function union(a, b) {
let unionSet = new Set(a);
for (let i of b) {
unionSet.add(i);
}
return unionSet;
}
// two sets of fruits
const setA = new Set(["apple", "mango", "orange"]);
const setB = new Set(["grapes", "apple", "banana"]);
const result = union(setA, setB);
console.log(result);
// perform intersection operation
// elements of set a that are also in set b
function intersection(setA, setB) {
let intersectionSet = new Set();
for (let i of setB) {
if (setA.has(i)) {
intersectionSet.add(i);
}
}
return intersectionSet;
}
// two sets of fruits
const setA = new Set(["apple", "mango", "orange"]);
const setB = new Set(["grapes", "apple", "banana"]);
const result = intersection(setA, setB);
console.log(result);
// perform difference operation
// elements of set a that are not in set b
function difference(setA, setB) {
let differenceSet = new Set(setA);
for (let i of setB) {
differenceSet.delete(i);
}
return differenceSet;
}
// two sets of fruits
const setA = new Set(["apple", "mango", "orange"]);
const setB = new Set(["grapes", "apple", "banana"]);
const result = difference(setA, setB);
console.log(result);
// perform subset operation
// true if all elements of set b is in set a
function subset(setA, setB) {
for (let i of setB) {
if (!setA.has(i)) {
return false;
}
}
return true;
}
// two sets of fruits
const setA = new Set(["apple", "mango", "orange"]);
const setB = new Set(["apple", "orange"]);
const result = subset(setA, setB);
console.log(result);