Files
algorithms/lib/intersectionSortedArray/intersectionSortedArray.ts
T

21 lines
380 B
TypeScript

export function intersectionSortedArray(a: number[], b: number[]): number[] {
let res: number[] = [];
let aI = 0;
let bI = 0;
while (aI < a.length && bI < b.length) {
let aValue = a[aI];
let bValue = b[bI];
if (aValue === bValue) {
res.push(aValue);
aI++;
bI++;
} else {
aValue < bValue ? aI++ : bI++;
}
}
return res;
};