Java Program to Find Union of two Arrays

Here you will get java program to find union of two arrays.

For example we have two sorted arrays a1[] = {2, 3, 5, 11} and a2[] = {4, 7, 9} then union of a1 and a2 will be {2, 3, 4, 5, 7, 9, 11}. A Java program for finding union of two arrays is given below.

Also Read: Java Program to Find Smallest and Largest Element in an Array

Java Program to Find Union of two Arrays

Output

Enter number of elements of first array:3
Enter number of elements of second array:5

Enter elements of first array in ascending order:2 4 6

Enter elements of second array in ascending order:1 3 7 9 12

Union of Arrays: 1 2 3 4 6 7 9 12

One thought on “Java Program to Find Union of two Arrays

  1. Дмитрий Згурский

    class Program
    {
    static void Main(string[] args)
    {
    int[] arrFirst = {2, 3, 5, 11};
    int[] arrSecond = {4, 7, 9};
    MyClass myClass = new MyClass();
    myClass.UnionTwoArrays(arrFirst, arrSecond);
    Console.ReadLine();
    }
    }

    class MyClass
    {
    private List doneList = new List();
    public void UnionTwoArrays(int[] first, int[] second)
    {
    foreach (int i in first)
    {
    doneList.Add(i);
    }
    foreach (int i in second)
    {
    doneList.Add(i);
    }
    doneList.Sort();
    foreach (int i in doneList)
    {
    Console.WriteLine(i);
    }
    }
    }

    Reply

Leave a Reply

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