Code Logo

Merge Sorted Arrays

Published at05 Jan 2026
Easy 1 views
Like2

In Merge Sorted Arrays, you are given two arrays that are already sorted in ascending order. Your task is to combine them into one new array that is also sorted in ascending order.

The important detail is that both input arrays are already ordered. That means you do not need to throw everything together and sort again from scratch. Instead, you can take advantage of the existing order while building the result.

For example, if nums1 = [1,3,5] and nums2 = [2,4,6], the correct output is [1,2,3,4,5,6]. The values from both arrays are interleaved into one sorted list. If nums1 = [] and nums2 = [1,2], the answer is simply [1,2] because there is nothing to merge from the first array. If the arrays contain duplicates, like [1,1] and [1,1], all four values should still appear in the result.

So the task is to combine the two sorted arrays while preserving ascending order and keeping every value.

Example Input & Output

Example 1
Input
nums1 = [1], nums2 = []
Output
[1]
Explanation

If one array is empty, the merged result is just the other array.

Example 2
Input
nums1 = [], nums2 = [2,3]
Output
[2,3]
Explanation

An empty first array contributes nothing, so the second array stays as the result.

Example 3
Input
nums1 = [1,2,3], nums2 = [2,5,6]
Output
[1,2,2,3,5,6]
Explanation

Values from both arrays are merged into one ascending result while keeping duplicates.

Algorithm Flow

Recommendation Algorithm Flow for Merge Sorted Arrays
Recommendation Algorithm Flow for Merge Sorted Arrays

Solution Approach

The best way to solve this problem is with the classic two-pointer merge technique. Because both input arrays are already sorted, you can walk through them from left to right and build the answer in one pass. This is more efficient and more natural than concatenating the arrays and sorting the whole thing again.

The idea is simple. Keep one pointer for nums1 and another pointer for nums2. At each step, compare the current values from both arrays. Whichever value is smaller should be added to the result first, because that value is the next smallest number available overall. Then move forward in the array you took the value from.

In JavaScript, the core merge logic looks like this:

const merged = [];
let i = 0;
let j = 0;

while (i < nums1.length && j < nums2.length) {
  if (nums1[i] <= nums2[j]) {
    merged.push(nums1[i]);
    i++;
  } else {
    merged.push(nums2[j]);
    j++;
  }
}

while (i < nums1.length) merged.push(nums1[i++]);
while (j < nums2.length) merged.push(nums2[j++]);

return merged;

The first loop handles the main comparison process while both arrays still have values left. The two smaller cleanup loops are important too. Once one array runs out, every remaining value in the other array can be appended directly, because that leftover portion is already sorted.

This approach works well for all of the common edge cases. If one array is empty, the result is just the other array. If both arrays contain duplicates, those duplicates are preserved because you never remove anything; you only choose the next smallest value each time. If one array has much larger numbers than the other, the comparison still naturally places them in the right order.

The time complexity is O(n + m), where n and m are the lengths of the two arrays, because each element is visited once. The space complexity is O(n + m) for the output array.

So the full strategy is: use two pointers, repeatedly take the smaller front value, append any leftovers at the end, and return the merged sorted array.

Best Answers

java
import java.util.ArrayList;
import java.util.List;
class Solution {
    public Object merge(Object nums1, Object nums2) {
        int[] arr1 = (int[]) nums1;
        int[] arr2 = (int[]) nums2;
        List<Integer> result = new ArrayList<>();
        int i = 0, j = 0;
        
        while (i < arr1.length && j < arr2.length) {
            if (arr1[i] <= arr2[j]) {
                result.add(arr1[i]);
                i++;
            } else {
                result.add(arr2[j]);
                j++;
            }
        }
        
        while (i < arr1.length) {
            result.add(arr1[i]);
            i++;
        }
        
        while (j < arr2.length) {
            result.add(arr2[j]);
            j++;
        }
        
        return result;
    }
}