es-next-tools
Version:
A comprehensive utility library for JavaScript and TypeScript that provides a wide range of functions for common programming tasks, including mathematical operations, date manipulations, array and object handling, string utilities, and more.
24 lines (23 loc) • 703 B
JavaScript
/**
* Returns an array of elements that are present in both input arrays.
* @param {T[]} array1 - The first array.
* @param {T[]} array2 - The second array.
* @returns {T[]} An array containing elements present in both input arrays.
* @template T
* @deprecated - Use Set.intersection() instead.
* @example
* const arr1 = [1, 2, 3, 4, 5];
* const arr2 = [3, 4, 5, 6, 7];
* const result = intersection(arr1, arr2); // [3, 4, 5]
*/
export function intersection(array1, array2) {
const result = [];
const setB = new Set(array2);
for (let i = 0; i < array1.length; i++) {
let z = array1[i];
if (setB.has(z))
result.push(z);
}
;
return result;
}