Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const arr1 = [1, 3, 5, 7, 11, 17, 21];
- const arr2 = [-1, 0, 2, 3, 9, 11, 29];
- function intersection(arr1, arr2) {
- let result = [];
- let p1 = 0, p2 = 0;
- while((p1 < arr1.length) && (p2 < arr2.length)) {
- if (arr1[p1] == arr2[p2]) {
- result.push(arr1[p1]);
- p1++;
- p2++;
- } else if (arr1[p1] < arr2[p2]) {
- p1++;
- } else {
- p2++;
- }
- }
- return result;
- }
- let try1 = intersection(arr1, arr2);
- console.log(try1);
Add Comment
Please, Sign In to add comment