Java program to merge two arrays



In the article, the users are going to merge two arrays. The users create any type of two arrays as input like arr1[] and arr2[] with the examples. The users have to create the two types of arrays with multiple elements as the inputs and it presents the result with different elements as the output. Print all of the elements of the array in the sorted sequence in Java programming language. Two approaches are illustrated in this article. The first approach uses the concept of the arrays whereas the second approach represents the Map function to merge to arrays. The benefit of using the Map function is to retrieve the final arrays that do not contain duplicate values.

Let's deep dive into this article, and know how it can be done by using Java programming language.

To show you some instances

Instance 1

Suppose the arrays as shown below

 I/P: arr1[]={2,1,8,5,7} I/P: arr2[]={9,6,6,3,1} O/P: arr3[]={1,1,2,3,5,6,6,7,8,9} 

Instance 2

Suppose the arrays as shown below

 I/P: arr3[]={8,8,0,6,6} I/P: arr4[]={7,7,0,0,4} O/P: arr3[]={4,6,6,7,7,8,8,0,0,0} 

Syntax

  • mergeArrays() this type of method is utilized to merge the arrays and print the outcome as a third array

  • Arrays.sort() this type of function is utilized to print the merged array and sort the items which are encompassed in two arrays.

Multiple Approaches

The users have divided the two approaches like this

  • By using Naïve approach

  • By using maps

Let's show the program along with the output one by one.

Note These programs might not offer the expected consequence on any online Java compiler. Online editors may not access your local organization's file track.

Approach 1: By using a naive approach

The users can display one array from two arrays which should be merged in the order. Create the items of the two arrays one by one and print the outcome as array 3.

Let's show the program along with the output.

Algorithm

  • Step 1 Begin

  • Step 2 Declare two arrays

  • Step 3 Save the items into the arrays

  • Step 4 Take the variables v and observe the length of the array

  • Step 5 Use the function mergeArrays() that can merge the arrays from the variables v and merge the data with two arrays

  • Step 6 Print the third array known as the output

  • Step 7 Print the result

Example

 import java.util.*; //Take a class called Main public class Main{ // define main function public static void main(String[] args){ //declare array int array1[]={1,5,4,8}; //Obtain the length of the array int v1=array1.length; int array2[]={2,8,9,0}; int v2=array2.length; //Merge the two arrays one by one into another array and print it int array3[]=new int[v1+v2]; //Use the function mergeArrays and insert the variables of the values mergeArrays(array1, array2, v1, v2, array3); System.out.println("after merging the two arrays"); // using for loop and go to the merging for(int w=0;w<v1+v2;w++) //Print the third array with merged data System.out.print(array3[w]+" "); } // define the method mergeArrays public static void mergeArrays(int[] array1, int[] array2, int v1, int v2, int[] array3){ int w=0; int b=0; int u=0; // take a while loop where v1 is greater than w while(w<v1){ array3[u++]=array1[w++]; } //using while loop while(b<v2){ array3[u++]=array2[b++]; } Arrays.sort(array3); } } 

Output

 After merging the two arrays 0 1 2 4 5 8 8 9 

Approach 2: By using maps

In this approach, the users used the maps to print the merged array as output. By using maps, add the items of both the arrays array 1 and array 2 as inputs. The users can print the inputs of the map. The users inserted Java libraries and import the classes from the Java package java.io.

Algorithm

  • Step 1 Begin

  • Step 2 Declare two arrays.

  • Step 3 Store the elements in the arrays.

  • Step 4 Take the variable o then go to the length of the array using for loop.

  • Step 5 Use the function mergeArrays() that can merge the arrays from the variables v and merge the data with two arrays.

  • Step 6 Use the Map method to merge the arrays.

  • Step 7 Print the third array known as output.

  • Step 8 Print the result.

Example

 // import the java libraries import java.io.*; import java.util.*; // take a class called Main public class Main{ //Define the method mergeArrays to merge the arrays public static void mergeArrays(int a[], int b[], int n, int m){ Map<Integer, Boolean>mw=new TreeMap<Integer, Boolean>(); // using for loop for(int o=0;o<n; o++){ mw.put(a[o], true); } //using for loop for(int o=0;o<m; o++){ mw.put(b[o], true); } for(Map.Entry<Integer,Boolean>me: mw.entrySet()){ // print the input System.out.print(me.getKey()+" "); } } //Define main function public static void main(String[] args){ //Create the arrays one by one int a[]={2,3,6,8}; int b[]={7,9,4,1}; //Take the length of one array int sz=a.length; // take the length of another array int sz1=b.length; mergeArrays(a, b, sz, sz1); } } 

Output

 1 2 3 4 6 7 8 9 

Conclusion

In this article, the users explored the approaches to merging the two types of arrays as inputs using Java programming language. The users merge the two arrays physically with Java libraries. The users restate the array to excerpt the data and facsimile them into another array in a sequence.

Updated on: 2023-11-21T17:53:28+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements