-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathintersection.js
More file actions
31 lines (26 loc) · 756 Bytes
/
intersection.js
File metadata and controls
31 lines (26 loc) · 756 Bytes
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
/**
* Intersection
*
* Compares two sets and produces a third set that contains all of the
* intersecting/matching values that are found in both sets
*
* Ex. Intersection of { 1, 2, 3 } and { 2, 3, 4 } is { 2, 3 }
*
* Performance: Quadratic - O(n * m), where n is the length of set 1 and m is the length of set 2
* Note: this is sort of like O(n^2) performance
*/
import { Set } from '../../../../data-structures/set/src/set'
export const intersection = (set1, set2) => {
const intersectionSet = new Set()
set1.enumerate().forEach(val => {
if (set2.has(val)) {
intersectionSet.add(val)
}
})
set2.enumerate().forEach(val => {
if (set1.has(val)) {
intersectionSet.add(val)
}
})
return intersectionSet
}