File tree Expand file tree Collapse file tree
Sprint-1/JavaScript/findCommonItems Expand file tree Collapse file tree Original file line number Diff line number Diff line change 99 * @param {Array } secondArray - Second array to compare
1010 * @returns {Array } Array containing unique common items
1111 */
12- export const findCommonItems = ( firstArray , secondArray ) => [
13- ...new Set ( firstArray . filter ( ( item ) => secondArray . includes ( item ) ) ) ,
14- ] ;
12+
13+ // export const findCommonItems = (firstArray, secondArray) => [
14+ // ...new Set(firstArray.filter((item) => secondArray.includes(item))),
15+ // ];
16+
17+ export const findCommonItems = ( firstArray , secondArray ) => {
18+ const firstArr = new Set ( firstArray ) ;
19+ const secondArr = new Set ( secondArray ) ;
20+
21+ // return [...firstArr].filter((arr) => secondArr.has(arr));
22+ return [ ...firstArr . intersection ( secondArr ) ] ;
23+ } ;
24+
25+ // * https://www.w3schools.com/js/js_set_methods.asp#mark_set_new
26+ // * Time Complexity: O(m+n)
27+ // * Space Complexity: O(m+n)
28+ // * Optimal Time Complexity: O(m+n)
29+ > >>> >>> 4541 adf ( intersection a built - in method )
You can’t perform that action at this time.
0 commit comments