This is a Java Program to Find Union & Intersection of 2 Arrays.

Enter size of both the arrays and then enter all the elements of both the arrays. Now we check for the elements of both the arrays. If element in first array is not present in second array then we put it in third array. Also if we put the common elements once in third array. This is for union of two arrays. For intersection we put only the common elements in fourth array.

Here is the source code of the Java Program to Find Union & Intersection of 2 Arrays. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

import java.util.Scanner;

public class Union_Intersection_Arrays

`{`

public static void main(String[] args)

`{`

int m, n, k, i = 0, flag = 0;

Scanner s = new Scanner(System.in);

System.out.print("Enter no. of elements you want in first array:");

m = s.nextInt();

System.out.print("Enter no. of elements you want in second array:");

n = s.nextInt();

k = m;

int a[] = new int[m];

int b[] = new int[n];

int c[] = new int[m+n];

int d[] = new int[m];

System.out.println("Enter all the elements in first array:");

for(i = 0; i < m; i++)

`{`

a[i] = s.nextInt();

`}`

System.out.println("Enter all the elements in second array:");

for(i = 0; i < n; i++)

`{`

b[i] = s.nextInt();

`}`

for(i = 0; i < m; i++)

`{`

c[i] = a[i];

`}`

for(i = 0; i < n; i++)

`{`

for(int j = 0; j < m; j++)

`{`

if(b[i] != a[j])

`{`

flag = 1;

`}`

`else`

`{`

flag = 0;

break;

`}`

`}`

if(flag == 1)

`{`

c[k] = b[i];

`k++;`

`}`

`}`

System.out.print("Union:");

for(i = 0; i < k - 1; i++)

`{`

System.out.print(c[i]+",");

`}`

System.out.print(c[k-1]);

System.out.println("");

int p = 0;

for(i = 0; i < n; i++)

`{`

for(int j = 0; j < m; j++)

`{`

if(b[i] == a[j])

`{`

flag = 1;

break;

`}`

`else`

`{`

flag = 0;

`}`

`}`

if(flag == 1)

`{`

d[p] = b[i];

`p++;`

`}`

`}`

System.out.print("Intersection:");

if(p > 1)

`{`

for(i = 0; i < p - 1; i++)

`{`

System.out.print(d[i]+",");

`}`

System.out.print(d[p-1]);

`}`

`else`

`{`

System.out.println("Null set");

`}`

`}`

`}`

Output:

$ javac Union_Intersection_Arrays.java $ java Union_Intersection_Arrays Enter no. of elements you want in first array:5 Enter no. of elements you want in second array:6 Enter all the elements in first array: 2 3 5 7 1 Enter all the elements in second array: 2 5 6 8 9 1 Union:2,3,5,7,1,6,8,9 Intersection:2,5,1

**Sanfoundry Global Education & Learning Series – 1000 Java Programs.**

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.