Skip to content

Commit 46fbc77

Browse files
authored
Create 1213-intersection-of-three-sorted-arrays.js
1 parent 6222698 commit 46fbc77

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {number[]} arr1
3+
* @param {number[]} arr2
4+
* @param {number[]} arr3
5+
* @return {number[]}
6+
*/
7+
const arraysIntersection = function(arr1, arr2, arr3) {
8+
const common = [], n1 = arr1.length, n2 = arr2.length, n3 = arr3.length
9+
let p1 = 0, p2 = 0, p3 = 0
10+
while(p1 < n1 && p2 < n2) {
11+
if(arr1[p1] === arr2[p2]) {
12+
common.push(arr1[p1])
13+
p1++
14+
p2++
15+
} else if(arr1[p1] < arr2[p2]) p1++
16+
else p2++
17+
}
18+
const res = [], nc = common.length
19+
let pc = 0
20+
while(pc < nc && p3 < n3) {
21+
if(common[pc] === arr3[p3]) {
22+
res.push(arr3[p3])
23+
pc++
24+
p3++
25+
} else if(common[pc] < arr3[p3]) pc++
26+
else p3++
27+
}
28+
29+
30+
return res
31+
};

0 commit comments

Comments
 (0)