Java Array Program · A07
Merge Two Arrays in Java
Create an array big enough for both inputs and copy them in one after the other. System.arraycopy() does the copying efficiently.
Problem Statement
Write a method that combines two int arrays into one new array: all elements of the first array, followed by all elements of the second.
The order inside each array is kept, and neither input is changed. Either array can be empty.
Input/Output Examples
[5, 1] + [9, 3, 7]Output[5, 1, 9, 3, 7]Explanation: the second array starts right after the last element of the first.[] + [4]Output[4]Explanation: the result is a copy of the second array.[] + []Output[]Explanation: the result is an empty array of length 0.Visual Explanation
[5, 1] and [9, 3, 7]: elements of the first array (purple) fill the start of the result, and the second array (amber) starts at index a.length.The result is created once with length a.length + b.length. The first copy starts at index 0, and the second starts at index a.length, so nothing is overwritten.
Approaches
Both approaches build a new array. The first copies memory blocks directly; the second joins two streams.
Approach 1: System.arraycopy() (Recommended)
Idea: allocate the result once, then copy each input into its own section with System.arraycopy().
- Create
resultwith lengtha.length + b.length. - Copy
atoresultstarting at index0. - Copy
btoresultstarting at indexa.length.
Time: O(n + m) · Space: O(n + m).
Approach 2: IntStream.concat()
Idea: turn both arrays into streams, join them with IntStream.concat() and collect the result with toArray().
- Create a stream for each array with
Arrays.stream(). - Join them with
IntStream.concat(first, second). - Collect the combined stream with
toArray().
Time: O(n + m) · Space: O(n + m).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: System.arraycopy() (Recommended)
The five arguments are: source, source start, destination, destination start and number of elements. Empty arrays copy zero elements without any special case.
import java.util.Arrays;
public class MergeArraysCopy {
// Copies a into the front of the result and b right after it
static int[] merge(int[] a, int[] b) {
int[] result = new int[a.length + b.length];
System.arraycopy(a, 0, result, 0, a.length);
System.arraycopy(b, 0, result, a.length, b.length);
return result;
}
public static void main(String[] args) {
int[][][] pairs = {{{5, 1}, {9, 3, 7}}, {{}, {4}}, {{}, {}}};
for (int[][] p : pairs) {
System.out.println(Arrays.toString(p[0]) + " + " + Arrays.toString(p[1])
+ " -> " + Arrays.toString(merge(p[0], p[1])));
}
}
}
[5, 1] + [9, 3, 7] -> [5, 1, 9, 3, 7] [] + [4] -> [4] [] + [] -> []
Time complexity: O(n + m) · Space complexity: O(n + m).
Approach 2: IntStream.concat()
A one-line version that reads naturally. It is slightly slower than arraycopy() because it processes elements one by one.
import java.util.Arrays;
import java.util.stream.IntStream;
public class MergeArraysStream {
// IntStream.concat() joins the two streams in order
static int[] merge(int[] a, int[] b) {
return IntStream.concat(Arrays.stream(a), Arrays.stream(b)).toArray();
}
public static void main(String[] args) {
int[][][] pairs = {{{5, 1}, {9, 3, 7}}, {{}, {4}}, {{}, {}}};
for (int[][] p : pairs) {
System.out.println(Arrays.toString(p[0]) + " + " + Arrays.toString(p[1])
+ " -> " + Arrays.toString(merge(p[0], p[1])));
}
}
}
[5, 1] + [9, 3, 7] -> [5, 1, 9, 3, 7] [] + [4] -> [4] [] + [] -> []
Time complexity: O(n + m) · Space complexity: O(n + m).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| System.arraycopy() | O(n + m) | O(n + m) |
| IntStream.concat() | O(n + m) | O(n + m) |
Here n and m are the lengths of the two arrays. Every element is copied once into a result array of size n + m.
Interview Tips and Recommended Approach
- Size the result correctly. The new array must have length
a.length + b.length; Java arrays cannot grow later. - Get the destination offset right. The second copy must start at
a.length, otherwise it overwrites the first array's elements. - Do not confuse this with merging sorted arrays. If the interviewer wants a sorted result, use two pointers to merge in order instead of copying blocks.
- Know the other copy tools:
Arrays.copyOf(a, a.length + b.length)creates a larger copy ofathat you can then fill withb.
Recommended Approach: System.arraycopy()
Why: it allocates the result once and copies each input in a single, highly optimised call. It is the standard way to combine arrays in Java and makes the index arithmetic easy to explain.