Created
January 18, 2022 08:18
-
-
Save jx13xx/22831203f11c756b8ae5fdb594c50a3b to your computer and use it in GitHub Desktop.
Comparing Two Arrays in Optimized Way 0(n)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function itemInCommon(arr1, arr2) { | |
let obj = {} | |
for(let i = 0; i < arr1.length; i++) | |
obj[arr1[i]] = true | |
for (let i = 0; i < arr1.length; i++) { | |
obj[arr1[i]] = true | |
} | |
for (let j = 0; j < arr2.length; j++) { | |
if (obj[arr2[j]]) return true | |
} | |
return false | |
} | |
console.log(itemInCommon(array1, array2)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment