Merge two arrays into a single sorted array using Java

Arrays are very important if you want to build a core foundation of programming. Be it any language, Arrays comes very handily in the data structure. In this article, we will share how to merge 2 different arrays into a single sorted array using Java language. We will also show the optimization being done using Java 8 Stream API library which will shorten the length of the program entirely.

Merge two arrays into a single sorted array using Java (using old versions of Java)

The code will consist of three different sections -
  1. Merge two arrays
  2. Remove duplicates
  3. Sorting the merged array
Step 1 - Merging two different arrays

//Defining mergedArray with combined size of arrayA and arrayB

int[] mergedArray = new int[arrayA.length + arrayB.length];

//Initializing pointers of arrayA, arrayB and mergedArray with 0

int i=0, j=0, k=0;

//Inserting all elements of arrayA into mergedArray

while (i < arrayA.length)
{
 mergedArray[k] = arrayA[i];
 k++;
 i++;
}

//Inserting all elements of arrayB into mergedArray

while (j < arrayB.length)
{
 mergedArray[k] = arrayB[j];
 k++;
 j++;
}

Step 2 - Removing duplicates using HashSet

//Defining one HashSet object called setWithNoDuplicates
//Remember, HashSet allows only unique elements

Set setWithNoDuplicates = new HashSet<>();

//Adding all elements of mergedArray into setWithNoDuplicates

for (int m = 0; m < mergedArray.length; m++)
{
 setWithNoDuplicates.add(mergedArray[m]);
}

//Now, setWithNoDuplicates will have only unique elements of mergedArray

//So, now iterate setWithNoDuplicates and
//add its elements into new array called mergedArrayWithNoDuplicates

Iterator it = setWithNoDuplicates.iterator();

int[] mergedArrayWithNoDuplicates = new int[setWithNoDuplicates.size()];

int n = 0;

//Adding all elements of setWithNoDuplicates into mergedArrayWithNoDuplicates

while (it.hasNext())
{
 mergedArrayWithNoDuplicates[n] = it.next();
 n++;
}

Step 3 - Sorting

Arrays.sort(mergedArrayWithNoDuplicates);



The same can be achieved using Java 8 in few lines of code as shown below -

import java.util.Arrays;
import java.util.stream.IntStream;
 
public class MergeTwoArraysAndRemoveDuplicatesProgram 
{   
    private static int[] mergeArraysAndRemoveDuplicates(int[] arrayA, int[] arrayB)
    {
        return IntStream.concat(IntStream.of(arrayA), IntStream.of(arrayB))
                        .distinct()
                        .sorted()
                        .toArray();
    }
     
    public static void main(String[] args)
    {
        int[] arrayA = new int[] {7, -5, 3, 8, -4, 11, -19, 21};
         
        int[] arrayB = new int[] {6, 13, -7, 0, 11, -4, 3, -5};
         
        int[] mergedArray = mergeArraysAndRemoveDuplicates(arrayA, arrayB);
         
        System.out.println("Array A : "+Arrays.toString(arrayA));
         
        System.out.println("Array B : "+Arrays.toString(arrayB));
         
        System.out.println("Sorted Merged Array With No Duplicates : ");
         
        System.out.println(Arrays.toString(mergedArray));
    }
}

We will come up with more articles on Java. Happy to share. Keep learning and keep sharing!

Comments

Popular posts from this blog

Learning Docker

Learning xpath concepts

Software Testing Life Cycle for Agile