Union of Arrays in Java

10
Union of Arrays in Java
Advertisement

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:

Program – Union of Arrays
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 HashSet back to an array, creating the union of unique elements.

Main Method:

  • Declares example arrays (array1 and array2) for demonstration purposes.
  • Calls the findUnion method, passing both arrays.
  • Prints the original arrays and the result of the union operation.

Output:

Output – Union of Arrays
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: