Skip to content

Instantly share code, notes, and snippets.

@AndSheCodes2
Created March 25, 2019 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndSheCodes2/4f1777fee39f54f1ca1e8399bbdc8ff1 to your computer and use it in GitHub Desktop.
Save AndSheCodes2/4f1777fee39f54f1ca1e8399bbdc8ff1 to your computer and use it in GitHub Desktop.
SortedArray
function sortedArrays(array1, array2){
const mergedArray = [];
let array1Item = array1[0];
let array2Item = array2[0];
let i = 1;
let j = 1;
if(array1.length === 0) {
return array2;
}
if(array2.length === 0) {
return array1;
}
while (array1Item || array2Item){
if(array2Item === undefined || array1Item < array2Item){
mergedArray.push(array1Item);
array1Item = array1[i];
i++;
}
else {
mergedArray.push(array2Item);
array2Item = array2[j];
j++;
}
}
return mergedArray;
}
sortedArrays([0,3,4,31], [3,4,6,30]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment