[HAI] week3 intersection array
https://www.interviewbit.com/problems/intersection-of-sorted-arrays/
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
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;
|
||||
};
|
||||
Reference in New Issue
Block a user