Introduction
The union of two arrays includes all unique elements from both arrays. This task involves combining the elements from both arrays and ensuring that duplicates are removed. This guide will walk you through writing a Java program that finds the union of two arrays.
Problem Statement
Create a Java program that:
- Prompts the user to enter the sizes and elements of two arrays.
- Finds and displays the union of the two arrays.
Example:
- Input:
- Array 1:
[1, 2, 3, 4, 5]
- Array 2:
[4, 5, 6, 7, 8]
- Array 1:
- Output:
"Union: [1, 2, 3, 4, 5, 6, 7, 8]"
Solution Steps
- Read the Sizes and Elements of the Two Arrays: Use the
Scanner
class to take the sizes and elements of the two arrays as input from the user. - Use a Data Structure to Track Unique Elements: Utilize a
HashSet
to store the unique elements from both arrays. - Find the Union: Add all elements from both arrays to the
HashSet
to ensure uniqueness. - Display the Result: Convert the
HashSet
to an array and print the union.
Java Program
// Java Program to Find the Union of Two Arrays // Author: https://www.rameshfadatare.com/ import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class ArrayUnion { public static void main(String[] args) { // Step 1: Read the size and elements of the first array from the user Scanner scanner = new Scanner(System.in); System.out.print("Enter the size of the first array: "); int size1 = scanner.nextInt(); int[] array1 = new int[size1]; System.out.println("Enter the elements of the first array:"); for (int i = 0; i < size1; i++) { array1[i] = scanner.nextInt(); } // Step 1: Read the size and elements of the second array from the user System.out.print("Enter the size of the second array: "); int size2 = scanner.nextInt(); int[] array2 = new int[size2]; System.out.println("Enter the elements of the second array:"); for (int i = 0; i < size2; i++) { array2[i] = scanner.nextInt(); } // Step 2: Find the union of the two arrays int[] unionArray = findUnion(array1, array2); // Step 3: Display the union System.out.println("Union of the two arrays:"); for (int element : unionArray) { System.out.print(element + " "); } } // Method to find the union of two arrays public static int[] findUnion(int[] array1, int[] array2) { Set<Integer> unionSet = new HashSet<>(); // Add all elements of the first array to the set for (int element : array1) { unionSet.add(element); } // Add all elements of the second array to the set for (int element : array2) { unionSet.add(element); } // Convert the set to an array int[] unionArray = new int[unionSet.size()]; int index = 0; for (int element : unionSet) { unionArray[index++] = element; } return unionArray; } }
Explanation
Step 1: Read the Sizes and Elements of the Two Arrays
- The
Scanner
class is used to read the sizes of the two arrays and their elements. ThenextInt()
method captures the sizes and each element.
Step 2: Find the Union of the Two Arrays
- The
findUnion()
method is used to find the union of the two arrays:- A
HashSet
namedunionSet
is used to store all unique elements from both arrays (array1
andarray2
). - Elements from both arrays are added to the
unionSet
. SinceHashSet
does not allow duplicate elements, this automatically ensures that only unique elements are stored.
- A
Step 3: Display the Union
- The program converts the
unionSet
to an array (unionArray
) and prints the elements of the union array using afor
loop.
Output Example
Example 1:
Enter the size of the first array: 5 Enter the elements of the first array: 1 2 3 4 5 Enter the size of the second array: 5 Enter the elements of the second array: 4 5 6 7 8 Union of the two arrays: 1 2 3 4 5 6 7 8
Example 2:
Enter the size of the first array: 4 Enter the elements of the first array: 10 20 30 40 Enter the size of the second array: 3 Enter the elements of the second array: 20 50 60 Union of the two arrays: 50 20 40 10 60 30
Example 3:
Enter the size of the first array: 6 Enter the elements of the first array: 2 4 6 8 10 12 Enter the size of the second array: 5 Enter the elements of the second array: 1 3 5 7 9 Union of the two arrays: 1 2 3 4 5 6 7 8 9 10 12
Conclusion
This Java program demonstrates how to find the union of two arrays using a HashSet
. The program efficiently combines elements from both arrays while automatically removing duplicates. This exercise is valuable for understanding how to work with sets and arrays in Java programming, particularly when dealing with operations that require uniqueness.