Java ProgramsArraysMerge Two Arrays

Merge Two Arrays in Java

intermediate·  Arrays  ·  Array Manipulation

Problem

Merging two sorted arrays means combining them into one array that's still fully sorted, without sorting the combined result from scratch.

Given two arrays that are each already sorted in ascending order, merge them into a single sorted array.

Input
[2, 5, 8, 12], [1, 4, 9, 10]
Output
[1, 2, 4, 5, 8, 9, 10, 12]

Java Program

Java
import java.util.Arrays; public class MergeTwoArrays { public static void main(String[] args) { int[] arr1 = {2, 5, 8, 12}; int[] arr2 = {1, 4, 9, 10}; int[] result = new int[arr1.length + arr2.length]; int i = 0, j = 0, k = 0; while (i < arr1.length && j < arr2.length) { if (arr1[i] <= arr2[j]) { // smaller (or equal) element goes next result[k++] = arr1[i++]; } else { result[k++] = arr2[j++]; } } // Copy whatever remains — it's already sorted since the source array was while (i < arr1.length) result[k++] = arr1[i++]; while (j < arr2.length) result[k++] = arr2[j++]; System.out.println(Arrays.toString(result)); } }

Output

[1, 2, 4, 5, 8, 9, 10, 12]

Core Logic

Walking both sorted arrays with a pointer each, always taking the smaller of the two current elements, builds the merged result already in sorted order.

How It Works
  1. 1Two pointers, i and j, start at index 0 in arr1 and arr2 respectively.
  2. 2While both pointers are still in bounds, whichever array's current element is smaller gets copied into the result next, and that array's pointer advances.
  3. 3Once one array runs out, whatever's left in the other array is already sorted, so the rest of it is copied over as-is.
  4. 4Because both input arrays were already sorted, comparing just the two current elements at each step is always enough to know which one belongs next.
Comparing 2 and 1 first takes 1; comparing 2 and 4 next takes 2 — this continues until every element from both arrays has been placed in order.
💡

Key Point: This is the exact merge step used inside merge sort — given two already-sorted arrays, it never needs to re-sort anything, just compare and pick.

Complexity
Time Complexity: O(n + m)Space Complexity: O(n + m)

Why: Each pointer walks its own array exactly once, and the result array holds every element from both inputs.

Key Concepts

two-pointer techniquemerge stepArrays.toString()

Approach 2: Java 8

Java
import java.util.Arrays; import java.util.stream.IntStream; public class MergeTwoArraysStream { public static void main(String[] args) { int[] arr1 = {2, 5, 8, 12}; int[] arr2 = {1, 4, 9, 10}; // Combines both arrays into one stream, then sorts the whole thing int[] merged = IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) .sorted() .toArray(); System.out.println(Arrays.toString(merged)); } }

Output

[1, 2, 4, 5, 8, 9, 10, 12]

Core Logic

Since sorted() has to run regardless, concatenating the two arrays and sorting the result gets to the same answer without writing the merge step by hand.

How It Works
  1. 1IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)) chains both arrays into a single stream.
  2. 2.sorted() sorts the combined stream into ascending order.
  3. 3.toArray() collects the sorted values back into a plain int[].
Concatenating [2, 5, 8, 12] and [1, 4, 9, 10] gives an eight-element stream, and sorted() arranges it into [1, 2, 4, 5, 8, 9, 10, 12].
💡

Key Point: This re-sorts the whole combined stream from scratch, ignoring the fact that both halves were already sorted — simpler to write, but it does more comparison work than the two-pointer merge.

Complexity
Time Complexity: O((n + m) log(n + m))Space Complexity: O(n + m)

Why: sorted() runs a full comparison sort over the combined stream instead of taking advantage of each half already being sorted, and the result array holds every element from both inputs.

Key Concepts

StreamIntStream.concat()sorted()

Related Programs