-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfindCommonItems.js
More file actions
31 lines (29 loc) · 1.11 KB
/
findCommonItems.js
File metadata and controls
31 lines (29 loc) · 1.11 KB
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
/**
* Finds common items between two arrays.
*
* Time Complexity: O(n + m)
* The old code used .includes() inside .filter(), which meant a loop inside a loop (O(n^2)).
* By using a 'Set', we can check if an item exists instantly (O(1)).
*
* Space Complexity: O(n) - We create a Set to store the items from the first array.
*
* Optimal Time Complexity: O(n) - We must look at every item at least once to compare them.
*
* @param {Array} firstArray - First array to compare
* @param {Array} secondArray - Second array to compare
* @returns {Array} Array containing unique common items
*/
export const findCommonItems = (firstArray, secondArray) => {
// Refactor: I put the first array into a Set for fast checking.
const itemsInFirst = new Set(firstArray);
const commonItems = new Set();
for (const item of secondArray) {
// Checking 'itemsInFirst.has(item)' is very fast O(1).
// The old 'includes()' was slow because it searched the whole list O(n).
if (itemsInFirst.has(item)) {
commonItems.add(item);
}
}
// Convert the Set back to an array to return it
return [...commonItems];
};