Union of Arrays in Java
In the realm of array manipulation, the union of two arrays stands as a powerful concept, bringing together unique elements from both arrays. This guide delves into the intricacies of finding the union of two arrays using Java. The provided Java program not only demonstrates this process but also highlights the versatility of Java in array operations.
Understanding the Union of Arrays:
The union of two arrays is the set of all distinct elements present in either of the arrays. This concept is akin to merging two worlds, preserving uniqueness while creating a harmonious blend of elements.
Java Program:
import java.util.Arrays;
import java.util.HashSet;
public class ArrayUnionFinder {
// Method: Find the union of two arrays
public static int[] findUnion(int[] array1, int[] array2) {
// Use HashSet to efficiently store unique elements
HashSet<Integer> unionSet = new HashSet<>();
// Add elements from the first array
for (int element : array1) {
unionSet.add(element);
}
// Add elements from the second array
for (int element : array2) {
unionSet.add(element);
}
// Convert the HashSet back to an array
int[] unionArray = new int[unionSet.size()];
int index = 0;
for (int uniqueElement : unionSet) {
unionArray[index++] = uniqueElement;
}
return unionArray;
}
// Main method: Test the union of arrays
public static void main(String[] args) {
// Example arrays for demonstration
int[] array1 = {1, 2, 3, 4, 5};
int[] array2 = {3, 4, 5, 6, 7};
// Call the findUnion method
int[] unionResult = findUnion(array1, array2);
// Print the original arrays and the union result
System.out.println("Array 1: " + Arrays.toString(array1));
System.out.println("Array 2: " + Arrays.toString(array2));
System.out.println("Union of Arrays: " + Arrays.toString(unionResult));
}
}Breaking Down the Implementation:
Method findUnion:
- Utilizes a HashSet (
unionSet) to efficiently store unique elements from both arrays. - Iterates through each array, adding elements to the
unionSet. - Converts the
HashSetback to an array, creating the union of unique elements.
Main Method:
- Declares example arrays (
array1andarray2) for demonstration purposes. - Calls the
findUnionmethod, passing both arrays. - Prints the original arrays and the result of the union operation.
Output:
Array 1: [1, 2, 3, 4, 5]
Array 2: [3, 4, 5, 6, 7]
Union of Arrays: [1, 2, 3, 4, 5, 6, 7]Learn more about Array.
Conclusion:
This Java program provides a robust solution for finding the union of two arrays. The HashSet-based approach ensures efficiency and uniqueness in the resulting union array. Embrace this Java technique to seamlessly bridge arrays and unlock new possibilities in array manipulation. Happy coding!
Latest Posts:
- Count Subarrays with Odd SumTitle: Count Subarrays with Odd Sum In this guide, you’ll learn how to count the subarrays of an integer array… Read more: Count Subarrays with Odd Sum
- Count Subarrays Whose Product is Less than KTitle: Count Subarrays Whose Product is Less than K In this guide, you’ll learn how to count the contiguous subarrays… Read more: Count Subarrays Whose Product is Less than K
- Minimum Number of Increments to Make Array Strictly IncreasingTitle: Minimum Number of Increments to Make Array Strictly Increasing In this guide, we will solve a classic greedy array… Read more: Minimum Number of Increments to Make Array Strictly Increasing
- Minimum Number of Operations to Make Array EqualTitle: Minimum Number of Operations to Make Array Equal In this guide, you’ll learn how to solve the Minimum Number… Read more: Minimum Number of Operations to Make Array Equal
- Minimum Number of Deletions to Make an Array SortedTitle: Minimum Number of Deletions to Make an Array Sorted Sorting problems often hide a subtler question: what is the… Read more: Minimum Number of Deletions to Make an Array Sorted
- Minimum Number of Swaps to Bring All Elements Less Than K TogetherTitle: Minimum Number of Swaps to Bring All Elements Less Than K Together In this guide, you’ll learn how to… Read more: Minimum Number of Swaps to Bring All Elements Less Than K Together



