let arr1 = ['first', 'second', 'three']
let arr2 = ['first']
new Set([...arr1, ...arr2]).size < arr1.length + arr2.length
arr1.find(v => arr2.includes(v))
function arrayHaveMatches(arr1, arr2) {
const arrays = [arr1, arr2]
const indexes = [0, 0]
let currArray = arr2[0] < arr1[0]
let val2 = arrays[!currArray][0]
while (indexes[currArray] < arrays[currArray].length) {
const val1 = arrays[currArray][indexes[currArray]]
if (val1 === val2) {
return true
} else if (val1 > val2) {
currArray = !currArray
val2 = val1
}
indexes[currArray] ++
}
return false
}
const hasCommon = (a, b) => undefined !== a.find(v => b.includes(v))
const tests = [
[[1,11,111], [2,3], false],
[[1,11,111,4,5], [2,3,4], true],
[[2,3,4], [1,11,111,4,5], true],
[[1,11,111,'A',{a:'aaa'}], [2,3,'a',{}], false],
[[1,11,111,undefined], [2,3,undefined], false],
[[1,11,111,2,undefined], [2,3,undefined], true],
];
tests.forEach(t => console.log(t[2] === hasCommon(...t)?'OK':'FAIL', JSON.stringify(t)))
function compare(arr1, arr2) {
let obj1 = {}; //Превращаем arr1 в ассоциативный obj1.
arr1.forEach(e=>obj1[e]=true);
return arr2.some(e=>e in obj1);
}
console.log(compare(arr1, arr2)); //Результат