How to merge two arrays in Java without Duplicates

In this tutorial, we will learn about how to merge two arrays in Java removing duplicate elements. Also, we will learn Taking Inputs in java or to initialize values in an array.

Removing Duplicates from the two different arrays sometimes becomes difficult and to make it in a simple task, we first merge them into one and then remove the duplicate from the resultant array. Merge Arrays without duplicates would be our main concern.

If you don’t know how to remove duplicates from arrays, then you are in the right place. Because, in this tutorial, we will learn not only how to remove duplicates from two merged arrays, also we will learn how to merge two of the arrays.

 Merge Two Arrays in Java without the duplicate elements in it

Initially, in the main() block, the first step is Besides Taking Two Arrays first of all with Given Initial Values. Calculate The lengths of Corresponding Arrays.

The Syntax for calculating length :

  • int variable_name=Array_name.length;

Besides we pass the Values of both Arrays and its lengths and Also The resultant array That is res[] in the Function SortArray. For the reason, we Pass the required parameters.

Most Noteworthy, In the Sort_array function, we First insert values for the First array and increment the values of the resultant array. Similarly, in likewise we then insert values of the second array into the resultant array and increase the value of resultant. Taking two arrays, the next task is to Merge Them in sorted order.

For sorting the arrays, any Sorting Algorithm can be implemented. Certainly, For reference, we can go for the below-mentioned link:

How to Sort an Array in Ascending Order in Java

In this Program, I Sorted The Arrays With the help of Arrays.Sort() function.Therefore, To sort a primitive array in ascending order, we pass our array to the sort method. Let’s learn this with some easy examples:

1
2
Arrays.sort(numbers);
assertArrayEquals(new int[] { -8, -2, 3, 5, 7, 9, 10 }, numbers);

 

Remove Duplicates from two arrays and merge them in Java

To store the index of the next unique element, while if the current element is not equal to the next element then store the next element. As a result, Implementing this concept will Remove the Duplicates From Merged Array. Further, we will Display This New Array Resultant into the main() block.

Below is the code for Merging Two arrays and removing Duplicates :

import java.util.*;
class RemoveDup 
{
  public static void SortArray(int a[], int b[], int res[], int n, int m)
        {
    int i=0, j=0, k=0;
    while (i < n){
      res[k] = a[i];
      i++;
      k++;
    }
     while (j < m) {
              res[k] = b[j];
              j++;
              k++;
          }
     Arrays.sort(res);
      
    }
  
  static int removeDuplicates(int res[], int a){
    
    if(a == 0 || a ==1 ){
      return a;
    }
    
    int j= 0 ;
    for(int i= 0; i <a-1; i++ )
        if(res[i] !=res [i+1])
        res[j++]= res [i];
    res[j++] = res [a-1];
    
    return j;
    
      
    }
  
   public static void main(String[] args) {
     
     int a[] = {9,9,10,10,1};
     int b[] = {81,71,1,4};
     int n = a.length;
     int m = b.length;	 
     int res[] = new int[ n + m];
     SortArray(a, b, res, n, m);
     System.out.println("Sorted merged list");
     
     for (int i= 0 ; i < n+ m ;i++){
       System.out.println( " "+ res[i]);
     }
     
     //------------------------------------------
     
     int h = res.length;
     h =removeDuplicates(res, h);
     
     System.out.println("Printing element after removing duplicates");
          for (int i=0; i<h; i++){
             System.out.println (res[i]+" ");
      }
  }
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *