Skip to main content

Program in Java: store 10 number in 2 different sda and merge them eliminating all the duplicate elements display them in ascending order

 import java.util.Scanner;
class merge {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the number of elements for the first array: : ");
        int m = sc.nextInt();
        System.out.println("Enter the number of elements for the second array: ");
        int n = sc.nextInt();
        int[] A = new int[m];
        int[] B = new int[n];
        int[] C = new int[m+n];
        System.out.println("Enter "+m+" into ");
        for(int i=0;i<m;i++)
        {
            A[i]=sc.nextInt();
        }
        System.out.println("Enter "+n+" into ");
        for(int i=0;i<n;i++)
        {
            B[i]=sc.nextInt();
        }
  //merging two arrays
        for(int i=0;i<A.length;i++)
  {
   C[i]=A[i];
  }
        for(int i=0;i<B.length;i++)
  {
   C[i+A.length]=B[i];
  }
  //sorting the array with bubblesort
  int k = C.length;
        for(int i=0;i<k-1;i++)
        {
            for(int j=0;j<k-i-1;j++)
            {
                if(C[j]>C[j+1])
                {
                    int t = C[j];
                    C[j] = C[j+1];
                    C[j+1] = t;
                }
            }
        }
  //extra array to store the new array without duplicate values
  int[] D = new int[C.length];
  int p=0; //counter variable
  k = D.length; //store the length of array D
  for(int i=0;i<k-1;i++)
  {
   if(C[i]!=C[i+1])
    D[p++]=C[i];
  }
  D[p++]=C[k-1];
  System.out.println("The new array without duplicates: ");
  for(int i=0;i<p;i++)
   System.out.println(D[i]);
    }
}

Popular posts from this blog