Guest User

Untitled

a guest
Jun 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.42 KB | None | 0 0
  1. const arr1 = [1, 3, 5, 7, 11, 17, 21];
  2. const arr2 = [-1, 0, 2, 3, 9, 11, 29];
  3.  
  4. function intersection(arr1, arr2) {
  5.  
  6. let result = [];
  7. let p1 = 0, p2 = 0;
  8.  
  9. while((p1 < arr1.length) && (p2 < arr2.length)) {
  10. if (arr1[p1] == arr2[p2]) {
  11. result.push(arr1[p1]);
  12. p1++;
  13. p2++;
  14. } else if (arr1[p1] < arr2[p2]) {
  15. p1++;
  16. } else {
  17. p2++;
  18. }
  19. }
  20.  
  21. return result;
  22. }
  23.  
  24. let try1 = intersection(arr1, arr2);
  25. console.log(try1);
Add Comment
Please, Sign In to add comment