A simple and straightforward O(m+n) solution.
const FindIntersection = array => { const commonNumbers = []; const array1 = array[0].split(", ").map(num => Number(num)); const array2 = array[1].split(", ").map(num => Number(num)); let index1 = 0; let index2 = 0; while (index1 < array1.length && index2 < array2.length) { if (array1[index1] < array2[index2]) { index1 += 1; } else if (array2[index2] < array1[index1]) { index2 += 1; } else { commonNumbers.push(array1[index1]); index1 += 1; index2 += 1; } } return commonNumbers.join(", "); };
EDIT: I just checked the next article and it has the same solution listed. Sorry I did not check that before posting this here.
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A simple and straightforward O(m+n) solution.
EDIT: I just checked the next article and it has the same solution listed. Sorry I did not check that before posting this here.