Skip to content

Instantly share code, notes, and snippets.

@iqbalfasri
Last active November 30, 2019 18:35
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 iqbalfasri/f0224b0215ea08b70ad6a1b7221748dc to your computer and use it in GitHub Desktop.
Save iqbalfasri/f0224b0215ea08b70ad6a1b7221748dc to your computer and use it in GitHub Desktop.
const arr1 = [1, 7, 13, 15];
const arr2 = [1, 4, 5, 9, 20];
const merge = (arr1, arr2) => {
let merged = [];
let index1 = 0;
let index2 = 0;
let current = 0;
while(current < (arr1.length + arr2.length)) {
let i = arr1[index1];
let j = arr2[index2];
if(i < j) {
merged[current] = i;
index1++
} else {
merged[current] = j;
index2++;
}
current++
}
return merged; // should be return [1, 1, 4, 5, 7, 9, 13, 15, 20]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment