-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathfindCommonItems.js
More file actions
25 lines (25 loc) · 906 Bytes
/
findCommonItems.js
File metadata and controls
25 lines (25 loc) · 906 Bytes
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
/**
* Finds common items between two arrays.
*
* Time Complexity:O(n+m) it build set and loop through the first array
* Space Complexity: O(m) space proportional to size of second array
* Optimal Time Complexity:O(m+n)
*
* @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) => [
// ...new Set(firstArray.filter((item) => secondArray.includes(item))),
// ];
// Refactored to use a Set for faster lookups, making the code more efficient
export const findCommonItems = (firstArray, secondArray) => {
const secondArraySet = new Set(secondArray);
const resultSet = new Set();
for (const element of firstArray) {
if (secondArraySet.has(element)) {
resultSet.add(element);
}
}
return [...resultSet];
};