How to merge two arrays in java

With two arrays, the task is to combine or combine them and store the result in 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 imagine array a and array b, then we'll store values in both the arrays.
  • After that, we'll calculate the length of arrays a and b and can store it into the variables let's imagine a1 and b1. we'd like to calculate the length of the array because by using the length of those arrays we will predict the length of the resultant array during which the weather are going to be store after merging.
  • Then by using System.arraycopy(), we merge both the arrays and therefore the result are going to be stored within 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]

Time Complexity: O(M + N)
Auxiliary Space: O(M + N)

Here, M is that the length of array a and N is that the length of array b.

Method 2: Without using pre-defined function

  • First, we initialize two arrays let's imagine array a and array b, then we'll store values in both the array.
  • After that, we'll calculate the length of both the arrays and can store it into the variables let's imagine a1 and b1. we'd like to calculate the length of the array because by using the length of those arrays we will predict the length of the resultant array during which the weather are going to be store after merging.
  • Then the new array c which is that the resultant array are going to be created.
  • Now, the primary loop is employed to store the weather of the primary array into the resultant array one by one and therefore the second for loop to store the weather of the second array into the resultant array one by one.
  • The final for loop is employed to print the weather 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

Time Complexity: O(M + N)
Auxiliary Space: O(M + N)

Here, M is the length of array and N is the length of array b.

Submit Your Programming Assignment Details