File tree Expand file tree Collapse file tree 1 file changed +2
-9
lines changed
Sprint-1/JavaScript/findCommonItems Expand file tree Collapse file tree 1 file changed +2
-9
lines changed Original file line number Diff line number Diff line change 66 * resulting in O(n * m) worst-case time.
77 *
88 * 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)
9+ * Space Complexity: O(m)
1210 * Optimal Time Complexity: O(n + m)
1311 *
1412 * @param {Array } firstArray - First array to compare
1715 */
1816export const findCommonItems = ( firstArray , secondArray ) => {
1917 const setB = new Set ( secondArray ) ;
20- const common = new Set ( ) ;
2118
22- for ( const item of firstArray ) {
23- if ( setB . has ( item ) ) common . add ( item ) ;
24- }
25-
26- return [ ...common ] ;
19+ return [ ...new Set ( firstArray . filter ( item => setB . has ( item ) ) ) ] ;
2720} ;
You can’t perform that action at this time.
0 commit comments