Skip to content

Instantly share code, notes, and snippets.

@Hurly77
Created April 16, 2021 02:12
Show Gist options
  • Save Hurly77/d32d226a938c3d51892894b03eae5135 to your computer and use it in GitHub Desktop.
Save Hurly77/d32d226a938c3d51892894b03eae5135 to your computer and use it in GitHub Desktop.
// check to see if one array is in another array.
const arrOne = ["a", "b", "c", "d", "e"]
const arrTwo = [1, "b", 2, 3, 4]
//we want to return true if any elments from array one exist inside array two.
//let define a funtion that can solve this problem
const compare = (arr1, arr2) => {
let hashMap = {}// set an empty object
for(let i = 0; i < arr1.length; i++){
if(!hashMap[arr1[i]]){
hashMap[arr1[i]] = true
}
}
for(let j = 0; j < arr2.length; j++){
if(hashMap[arr2[j]]){
return true
}
}
return false
}
compare(arrOne, arrTwo)
//=> true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment