* Returns a new set containing only elements present in both sets. * @param otherSet - The set to intersect with * @returns A new set with common elements * @complexity Time O(n) | Space O(n)
(otherSet: MySet)
| 121 | * @complexity Time O(n) | Space O(n) |
| 122 | */ |
| 123 | intersection(otherSet: MySet): MySet { |
| 124 | const intersectionSet = new MySet(); |
| 125 | const [smallerSet, largerSet] = this.size <= otherSet.size ? [this, otherSet] : [otherSet, this]; |
| 126 | smallerSet.values().forEach(value => { |
| 127 | if (largerSet.has(value)) { |
| 128 | intersectionSet.add(value); |
| 129 | } |
| 130 | }); |
| 131 | return intersectionSet; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns a new set with elements in this set but not in otherSet. |
no test coverage detected