We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 65945ef commit 99918a5Copy full SHA for 99918a5
1 file changed
Sprint-1/JavaScript/findCommonItems/findCommonItems.js
@@ -6,9 +6,7 @@
6
* resulting in O(n * m) worst-case time.
7
*
8
* 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)
+ * Space Complexity: O(m)
12
* Optimal Time Complexity: O(n + m)
13
14
* @param {Array} firstArray - First array to compare
@@ -17,11 +15,6 @@
17
15
*/
18
16
export const findCommonItems = (firstArray, secondArray) => {
19
const setB = new Set(secondArray);
20
- const common = new Set();
21
22
- for (const item of firstArray) {
23
- if (setB.has(item)) common.add(item);
24
- }
25
-
26
- return [...common];
+ return [...new Set(firstArray.filter(item => setB.has(item)))];
27
};
0 commit comments