-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathset.ts
38 lines (35 loc) · 831 Bytes
/
set.ts
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
/**
* Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set#implementing_basic_set_operations
*/
/**
* returns intersection of two sets
*/
export function intersection<T>(setA: Set<T>, setB: Set<T>) {
const _intersection = new Set<T>();
for (const elem of setB) {
if (setA.has(elem)) {
_intersection.add(elem);
}
}
return _intersection;
}
/**
* returns union of two sets
*/
export function union<T>(setA: Set<T>, setB: Set<T>) {
const _union = new Set(setA);
for (const elem of setB) {
_union.add(elem);
}
return _union;
}
/**
* returns difference of two sets
*/
export function difference<T>(setA: Set<T>, setB: Set<T>) {
const _difference = new Set(setA);
for (const elem of setB) {
_difference.delete(elem);
}
return _difference;
}