Related Articles
Java Program to Merge Two Arrays
• Last Updated : 15 Oct, 2020

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 lets say array a and array b, then we will store values in both the arrays.
• After that, we will calculate the length of arrays a and b and will store it into the variables lets 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 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

 `// 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 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 lets 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 lets 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

 `// 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 a and N is the length of array b.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up