JavaScript Program to Merge Two Sorted Arrays
function mergeSortedArrays(arr1, arr2) { let i = 0, j = 0, merged = []; while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) merged.push(arr1[i++]); else merged.push(arr2[j++]); } return merged.concat(arr1.slice(i)).concat(arr2.slice(j)); }.Examples
How to Think About It
Algorithm
Code
function mergeSortedArrays(arr1, arr2) { let i = 0, j = 0; const merged = []; while (i < arr1.length && j < arr2.length) { if (arr1[i] < arr2[j]) { merged.push(arr1[i]); i++; } else { merged.push(arr2[j]); j++; } } return merged.concat(arr1.slice(i)).concat(arr2.slice(j)); } console.log(mergeSortedArrays([1,3,5], [2,4,6]));
Dry Run
Let's trace merging [1,3,5] and [2,4,6] through the code
Start with pointers i=0, j=0
arr1[i]=1, arr2[j]=2, merged=[]
Compare arr1[i] and arr2[j]
1 < 2, so push 1, i=1, merged=[1]
Compare arr1[i] and arr2[j]
3 > 2, so push 2, j=1, merged=[1,2]
Compare arr1[i] and arr2[j]
3 < 4, so push 3, i=2, merged=[1,2,3]
Compare arr1[i] and arr2[j]
5 > 4, so push 4, j=2, merged=[1,2,3,4]
Compare arr1[i] and arr2[j]
5 < 6, so push 5, i=3, merged=[1,2,3,4,5]
arr1 exhausted, add remaining arr2 elements
merged=[1,2,3,4,5,6]
| i | j | merged |
|---|---|---|
| 0 | 0 | [] |
| 1 | 0 | [1] |
| 1 | 1 | [1,2] |
| 2 | 1 | [1,2,3] |
| 2 | 2 | [1,2,3,4] |
| 3 | 2 | [1,2,3,4,5] |
| 3 | 3 | [1,2,3,4,5,6] |
Why This Works
Step 1: Compare elements
The code compares the current elements of both arrays using if (arr1[i] < arr2[j]) to decide which to add next.
Step 2: Add smaller element
It adds the smaller element to the merged array and moves the pointer forward in that array.
Step 3: Add remaining elements
After one array is fully added, the remaining elements of the other array are concatenated to the result using concat.
Alternative Approaches
function mergeSortedArrays(arr1, arr2) { return arr1.concat(arr2).sort((a,b) => a - b); } console.log(mergeSortedArrays([1,3,5], [2,4,6]));
function mergeSortedArrays(arr1, arr2) { if (!arr1.length) return arr2; if (!arr2.length) return arr1; if (arr1[0] < arr2[0]) { return [arr1[0]].concat(mergeSortedArrays(arr1.slice(1), arr2)); } else { return [arr2[0]].concat(mergeSortedArrays(arr1, arr2.slice(1))); } } console.log(mergeSortedArrays([1,3,5], [2,4,6]));
Complexity: O(n + m) time, O(n + m) space
Time Complexity
The algorithm goes through each element of both arrays once, so it takes time proportional to the sum of their lengths.
Space Complexity
It creates a new array to hold all elements, so space used is proportional to the total number of elements.
Which Approach is Fastest?
The two-pointer merge is fastest because it avoids sorting the combined array, unlike the concat-and-sort method.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Two-pointer merge | O(n + m) | O(n + m) | Merging already sorted arrays efficiently |
| Concat and sort | O((n + m) log(n + m)) | O(n + m) | Quick code but slower for large arrays |
| Recursive merge | O(n + m) | O(n + m) | Readable code but less efficient due to recursion overhead |