Raw Code
/**
* Return a new array that contains the intersection of two arrays: items that are in both arrays
*/
export function intersectArrays<TArrayItem>(
a: TArrayItem[],
b: TArrayItem[],
) {
// early exits if one of the array has length 0
if (a.length === 0 || b.length === 0) return [];
const [shorter, longer] = a.length < b.length ? [a, b] : [b, a];
const longerSet = new Set(longer);
return shorter.filter(item => longerSet.has(item));
}