Skip to content

Instantly share code, notes, and snippets.

@jx13xx
Created January 18, 2022 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jx13xx/22831203f11c756b8ae5fdb594c50a3b to your computer and use it in GitHub Desktop.
Save jx13xx/22831203f11c756b8ae5fdb594c50a3b to your computer and use it in GitHub Desktop.
Comparing Two Arrays in Optimized Way 0(n)
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