const one = ['one', 'two', 'three', 'four', 'five']
const two = ['a', 'b', 'five', 'c', 'one']
['five', 'one']
let one = ['one', 'two', 'three', 'four', 'five'];
let two = ['a', 'b', 'five', 'c', 'one'];
const [long, short] = one.length > two.length ? [one,two] : [two,one];
short.sort();
const shortLength = short.length;
const binSearch = needle => {
let start = 0, finish = shortLength - 1;
while (start <= finish) {
const center = Math.floor((start + finish) / 2);
if (short[center] < needle) start = center + 1;
else if (short[center] > needle) finish = center - 1;
else return true;
}
return false;
}
const result = [];
for (let i = 0, length = long.length; i < length; i++)
if (binSearch(long[i])) result.push(long[i]);
result // ["five","one"]
let one = ['one', 'two', 'three', 'four', 'five'];
let two = ['a', 'b', 'five', 'c', 'one'];
one.sort();
two.sort();
let i = one.length, j = two.length, three = [];
while (i > 0 && j > 0) {
i--;
j--;
if (one[i] > two[j]) j++;
else if (one[i] < two[j]) i++;
else three.push(one[i]);
}
console.log(three);
Можно воспользоваться библиотекой:const _ = require("lodash");
console.log(_.intersection(one, two));
Можно воспользоваться встроенным классом Set (пусть будет задание на дом).function checkArrayIntersection(array1: string[], array2: string[]): boolean {
return array1.some(item => array2.includes(item));
}
// Пример использования
const array1 = ["apple", "banana", "orange"];
const array2 = ["pear", "grape", "banana"];
const hasIntersection = checkArrayIntersection(array1, array2);
console.log(hasIntersection); // Выводит true, так как 'banana' есть в обоих массивах