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
import java.util.Arrays;
public class MergeTwoArrays1 {
public static void main(String[] args)
{
int [] a = { 10 , 20 , 30 , 40 };
int [] b = { 50 , 60 , 70 , 80 };
int a1 = a.length;
int b1 = b.length;
int c1 = a1 + b1;
int [] c = new int [c1];
System.arraycopy(a, 0 , c, 0 , a1);
System.arraycopy(b, 0 , c, a1, b1);
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
import java.io.*;
public class MergeTwoArrays2 {
public static void main(String[] args)
{
int a[] = { 30 , 25 , 40 };
int b[] = { 45 , 50 , 55 , 60 , 65 };
int a1 = a.length;
int b1 = b.length;
int c1 = a1 + b1;
int [] c = new int [c1];
for ( int i = 0 ; i < a1; i = i + 1 ) {
c[i] = a[i];
}
for ( int i = 0 ; i < b1; i = i + 1 ) {
c[a1 + i] = b[i];
}
for ( int i = 0 ; i < c1; i = i + 1 ) {
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
15 Oct, 2020
Like Article
Save Article