Skip to content

Instantly share code, notes, and snippets.

@rafaelquintanilha
rafaelquintanilha / merge.js
Created April 5, 2016 13:57
Merge two already sorted arrays
function merge(arr1, arr2) {
var result = []; // Array where elements will be added
var i1 = 0; // Index for arr1
var i2 = 0; // Index for arr2
// Iterate over arrays until reach the end of one of them
while ( i1 < arr1.length && i2 < arr2.length ) {
arr1[i1] < arr2[i2] ? result.push(arr1[i1++]) : result.push(arr2[i2++]) // Push the smallest and increment
}