This is the Java Program to Find the Union and Intersection of 2 Arrays.

Given two arrays of integers, find and print the union and intersection of the arrays.

Example:

Array: [1,2,3,4,5]

Array1: [5,3,6,7,9]

Output:

Union = [1,2,3,4,5,6,7,9]

Intersection = [3,5]

**Create two sets called union and intersection**. To find the union, add the first array’s elements to the set union. Now, add all the second array elements if they are not present in the set union. To find the intersection, find the common elements and add them to the set.

Here is the source code of the Java Program to Find the Union and Intersection of 2 Arrays. The program is successfully compiled and tested using IDE IntelliJ Idea in Windows 7. The program output is also shown below.

`// Java Program to Find the Union and Intersection of 2 Arrays.`

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.Arrays;

import java.util.HashSet;

import java.util.Set;

public class UnionAndIntersection {

`// Function to find and display the union and intersection`

static void displayUnionAndIntersection(int[] arrayOne,int[] arrayTwo){

Set<Integer> obj = new HashSet<>();

int i,j;

for(i=0; i<arrayOne.length; i++){

obj.add(arrayOne[i]);

`}`

for(j=0; j<arrayTwo.length; j++){

obj.add(arrayTwo[j]);

`}`

System.out.println("The union of both the arrays is");

for(Integer I: obj){

System.out.print(I + " ");

`}`

System.out.println();

obj.clear();

System.out.println("The intersection of both the arrays is");

for(i=0; i<arrayOne.length; i++){

obj.add(arrayOne[i]);

`}`

for(j=0; j<arrayTwo.length; j++){

if(obj.contains(arrayTwo[j]))

System.out.print(arrayTwo[j] + " ");

`}`

`}`

`// Function to read the input`

public static void main(String[] args) {

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

int m,n;

System.out.println("Enter the size of the two arrays");

try {

n = Integer.parseInt(br.readLine());

m = Integer.parseInt(br.readLine());

`}`

catch (IOException e)

`{`

System.out.println("Invalid input");

return;

`}`

int[] arrayOne = new int[n];

int[] arrayTwo = new int[m];

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

int i,j;

for(i=0; i<arrayOne.length; i++){

try {

arrayOne[i] = Integer.parseInt(br.readLine());

`}`

catch (IOException e)

`{`

System.out.println("Invalid array element. Enter it again");

`i--;`

`}`

`}`

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

for(i=0; i<arrayTwo.length; i++){

try {

arrayTwo[i] = Integer.parseInt(br.readLine());

`}`

catch (IOException e)

`{`

System.out.println("Invalid array element. Enter it again");

`i--;`

`}`

`}`

displayUnionAndIntersection(arrayOne,arrayTwo);

`}`

`}`

1. In function displayUnionAndIntersection(), a hash set object is created, which follows all the properties of a set.

2. The loops for(j=0; j<arrayTwo.length; j++) and for(i=0; i<arrayOne.length; i++) adds the elements of the two arrays to the HashSet. An element if already present in the HashSet is not added automatically.

3. The loop for(Integer I: obj) is a for-each loop and it displays the union of the two arrays. Next, the HashSet is cleared.

4. Now the elements of the arrayOne are again added to the HashSet.

5. The last loop for(j=0; j<arrayTwo.length; j++) displays the intersection of the two arrays.

**Time Complexity: O(n + m)** where n is the number of elements in the first array and m is the number of elements in the second array.

Case 1 (Simple Test Case): Enter the size of the two arrays 5 5 Enter the first array elements 1 2 3 4 5 Enter the second array elements 5 3 6 7 9 The union of both the arrays is 1 2 3 4 5 6 7 9 The intersection of both the arrays is 5 3 Case 2 (Simple Test Case - entirely different arrays): Enter the size of the two arrays 4 4 Enter the first array elements 1 2 3 4 Enter the second array elements 6 7 8 9 The union of both the arrays is 1 2 3 4 6 7 8 9 The intersection of both the arrays is Case 3 (Simple Test Case - entirely same arrays): Enter the size of the two arrays 4 4 Enter the first array elements 1 2 3 4 Enter the second array elements 1 2 3 4 The union of both the arrays is 1 2 3 4 The intersection of both the arrays is 1 2 3 4

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

**Next Steps:**

- Get Free Certificate of Merit in Java Programming
- Participate in Java Programming Certification Contest
- Become a Top Ranker in Java Programming
- Take Java Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

**Related Posts:**

- Practice BCA MCQs
- Practice Information Technology MCQs
- Buy Programming Books
- Buy Java Books
- Apply for Information Technology Internship