Java Program to Merge Two Arrays
Given two arrays, the task is to merge or concatenate them and store the result into another array.
Examples:
Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8}
Output: arr3[] = {1, 3, 4, 5, 2, 4, 6, 8}Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8}
Output: arr3[] = {5, 8, 9, 4, 7, 8}
Method 1: Using Predefined function
- First, we initialize two arrays let’s say array a and array b, then we will store values in both arrays.
- After that, we will calculate the length of arrays a and b and will store it in the variables let’s say a1 and b1. We need to calculate the length of the array because by using the length of these arrays we can predict the length of the resultant array in which the elements will be stored after merging.
- Then by using System.arraycopy(), we merge both the arrays and the result will be stored in the third array.
Below is the implementation of the above approach.
// Java Program to demonstrate merging
// two array using pre-defined method
import java.util.Arrays;
public class MergeTwoArrays1 {
public static void main(String[] args)
{
// first array
int[] a = { 10, 20, 30, 40 };
// second array
int[] b = { 50, 60, 70, 80 };
// determines length of firstArray
int a1 = a.length;
// determines length of secondArray
int b1 = b.length;
// resultant array size
int c1 = a1 + b1;
// create the resultant array
int[] c = new int[c1];
// using the pre-defined function arraycopy
System.arraycopy(a, 0, c, 0, a1);
System.arraycopy(b, 0, c, a1, b1);
// prints the resultant array
System.out.println(Arrays.toString(c));
}
}
Output
[10, 20, 30, 40, 50, 60, 70, 80]
Complexity of the above Method:
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)Here, M is the length of array a and N is the length of array b.
Method 2: Without using pre-defined function
- First, we initialize two arrays let’s say array a and array b, then we will store values in both the array.
- After that, we will calculate the length of both the arrays and will store it into the variables let’s say a1 and b1. We need to calculate the length of the array because by using the length of these arrays we can predict the length of the resultant array in which the elements will be store after merging.
- Then the new array c which is the resultant array will be created.
- Now, the first loop is used to store the elements of the first array into the resultant array one by one and the second for loop to store the elements of the second array into the resultant array one by one.
- The final for loop is used to print the elements of the resultant array.
Below is the implementation of the above approach.
// Java Program to demonstrate merging
// two array without using pre-defined method
import java.io.*;
public class MergeTwoArrays2 {
public static void main(String[] args)
{
// first array
int a[] = { 30, 25, 40 };
// second array
int b[] = { 45, 50, 55, 60, 65 };
// determining length of first array
int a1 = a.length;
// determining length of second array
int b1 = b.length;
// resultant array size
int c1 = a1 + b1;
// Creating a new array
int[] c = new int[c1];
// Loop to store the elements of first
// array into resultant array
for (int i = 0; i < a1; i = i + 1) {
// Storing the elements in
// the resultant array
c[i] = a[i];
}
// Loop to concat the elements of second
// array into resultant array
for (int i = 0; i < b1; i = i + 1) {
// Storing the elements in the
// resultant array
c[a1 + i] = b[i];
}
// Loop to print the elements of
// resultant array after merging
for (int i = 0; i < c1; i = i + 1) {
// print the element
System.out.println(c[i]);
}
}
}
Output
30 25 40 45 50 55 60 65
Complexity of the above Method:
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)Here, M is the length of array a and N is the length of array b.
Method 3: Using Java Streams
This method utilizes Java Streams to concatenate or merge two arrays. Java Streams were introduced in Java 8 and provide a declarative way to process collections of elements.
Here’s a breakdown of how the Java Streams approach works:
// Java Program to demonstrate merging
// two array using Java Stream
import java.util.Arrays;
import java.util.stream.IntStream;
// Driver Class
public class MergeTwoArraysUsingStreams {
// main function
public static void main(String[] args) {
// First array
int a[] = {30, 25, 40};
// Second array
int b[] = {45, 50, 55, 60, 65};
// Merging arrays using Java Streams
int[] c = mergeArraysUsingStreams(a, b);
// Print the elements of the merged array
Arrays.stream(c).forEach(System.out::println);
}
public static int[] mergeArraysUsingStreams(int[] arr1, int[] arr2) {
return IntStream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();
}
}
Output
30 25 40 45 50 55 60 65
Complexity of the above Method:
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)Here, M is the length of array a and N is the length of array b.
Method 4: Using ArrayList
In this method, we have used an ArrayList to facilitate the merging of two arrays. An ArrayList is a dynamic data structure in Java that can grow or shrink in size as needed.
- First, create an ArrayList to store the merged elements.
- Then, iterate through first Array and add each element to resultList.
- After that, iterate through the second Array and add each element to resultList.
- Now, convert the ArrayList to an array using streams.
- Then merge and display the merged array.
Below is the implementation of the above approach:
// Java Program to demonstrate merging
// two array using ArrayList
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
// Driver Class
public class MergeArrays {
public static int[] mergeArraysUsingArrayList(int[] a,
int[] b)
{
// Create an ArrayList to store the merged
// elements
List<Integer> resultList = new ArrayList<>();
// Iterate through a and add each element to
// resultList
for (int num : a) {
resultList.add(num);
}
// Iterate through b and add each element to
// resultList
for (int num : b) {
resultList.add(num);
}
// Convert the ArrayList to an array using
// streams
return resultList.stream()
.mapToInt(Integer::intValue).toArray();
}
// Main Function
public static void main(String[] args)
{
// Example usage
int a[] = { 30, 25, 40 };
int b[] = { 45, 50, 55, 60, 65 };
int[] result = mergeArraysUsingArrayList(a, b);
// Display the merged array
for (int i = 0; i < result.length; i = i + 1) {
System.out.println(result[i]);
}
}
}
Output
30 25 40 45 50 55 60 65
Complexity of the above Method:
Time Complexity: O(M + N)
Auxiliary Space: O(M + N)Here, M is the length of array a and N is the length of array b.
Contact Us