Skip to content

Commit 99918a5

Browse files
committed
Refactor findCommonItems function to simplify implementation
1 parent 65945ef commit 99918a5

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

Sprint-1/JavaScript/findCommonItems/findCommonItems.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
* resulting in O(n * m) worst-case time.
77
*
88
* Time Complexity: O(n + m) average
9-
* Space Complexity: O(m + k) where:
10-
* - m = secondArray length (Set storage)
11-
* - k = number of unique common items (result Set storage)
9+
* Space Complexity: O(m)
1210
* Optimal Time Complexity: O(n + m)
1311
*
1412
* @param {Array} firstArray - First array to compare
@@ -17,11 +15,6 @@
1715
*/
1816
export const findCommonItems = (firstArray, secondArray) => {
1917
const setB = new Set(secondArray);
20-
const common = new Set();
2118

22-
for (const item of firstArray) {
23-
if (setB.has(item)) common.add(item);
24-
}
25-
26-
return [...common];
19+
return [...new Set(firstArray.filter(item => setB.has(item)))];
2720
};

0 commit comments

Comments
 (0)