Created
July 23, 2019 11:23
-
-
Save anushshukla/8bdda6597a109762ea325161877c1cd8 to your computer and use it in GitHub Desktop.
Merge 2 arrays and sort it
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
| // [1, 3, 6, 9,10, 2, 5] and [3,1,9,4,17,18,10] | |
| // [1, 2, 3, 6, 9, 10, 17, 18] | |
| //merge | |
| //return one array sorted [] | |
| function getSortedArr(arr) { | |
| var a = []; | |
| for (var i = 0; i < arr.length; i++) { | |
| if (arr[i] > arr[i+1]) { | |
| } | |
| } | |
| } | |
| function getSortedUniqueArr(a1, a2) { | |
| var a = []; | |
| var highestLength = a1.length > a2.length ? a1.length : a2.length; | |
| for (var i = 0; i < highestLength; i++) { | |
| var add1 = a1[i] < a2[i] ? a1[i] : a2[i]; | |
| var add2 = a1[i] > a2[i] ? a1[i] : a2[i]; | |
| !a.includes(add1) && a.push(add1); | |
| !a.includes(add2) && a.push(add2); | |
| } | |
| return getSortedArr(arr); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment