Created
June 17, 2018 23:00
-
-
Save pinkmomo027/2ac79f707d76916c24f9b2bb827d81d0 to your computer and use it in GitHub Desktop.
find common elements
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); |
Author
pinkmomo027
commented
Jun 17, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment