(sets)
| 12 | * @returns {Set<T>} returns a new Set containing the intersecting items |
| 13 | */ |
| 14 | const intersect = (sets) => { |
| 15 | if (sets.length === 0) return new Set(); |
| 16 | if (sets.length === 1) return new Set(sets[0]); |
| 17 | let minSize = Infinity; |
| 18 | let minIndex = -1; |
| 19 | for (let i = 0; i < sets.length; i++) { |
| 20 | const size = sets[i].size; |
| 21 | if (size < minSize) { |
| 22 | minIndex = i; |
| 23 | minSize = size; |
| 24 | } |
| 25 | } |
| 26 | const current = new Set(sets[minIndex]); |
| 27 | for (let i = 0; i < sets.length; i++) { |
| 28 | if (i === minIndex) continue; |
| 29 | const set = sets[i]; |
| 30 | for (const item of current) { |
| 31 | if (!set.has(item)) { |
| 32 | current.delete(item); |
| 33 | } |
| 34 | } |
| 35 | } |
| 36 | return current; |
| 37 | }; |
| 38 | |
| 39 | /** |
| 40 | * Checks if a set is the subset of another set |
no test coverage detected