Skip to content

Commit 2d34b6d

Browse files
committed
update logic so that to avoid duplciation and only unique common numbers found in both arrays (1&2) are stored in uniqueCommonItems
1 parent c757b24 commit 2d34b6d

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

Sprint-1/JavaScript/findCommonItems/findCommonItems.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@
1818

1919
export const findCommonItems = (firstArray, secondArray) => {
2020
const arrayToSet = new Set(secondArray);
21+
const uniqueCommonItems = [];
22+
const checkedItems = new Set();
23+
2124
for (const element of firstArray) {
22-
if (secondArray.includes(element)) {
23-
arrayToSet.add(element);
25+
if (arrayToSet.has(element) && !checkedItems.has(element)) {
26+
checkedItems.add(element);
27+
uniqueCommonItems.push(element);
2428
}
2529

2630
}
27-
return arrayToSet;
31+
return uniqueCommonItems;
2832
};

0 commit comments

Comments
 (0)