Intersection of two sorted arrays
2022年5月1日小于 1 分钟
Intersection of two sorted arrays
Question
Given 2 sorted array of integers, find the elements that exist in both arrays.
intersect(
[1,2,2,3,4,4],
[2,2,4,5,5,6,2000]
)
// [2,2,4]
- The arrays might have duplicate numbers.
- The order of returning result doesn't matter.
- What is the time & space cost of your approach? Could you improve it?
Code
/**
* @param {number[]} arr1 - integers
* @param {number[]} arr2 - integers
* @returns {number[]}
*/
function intersect(arr1, arr2) {
// your code here
}