Array Intersection in Java

13
Array Intersection in Java
Advertisement

Array Intersection in Java

In the realm of array manipulation, identifying the intersection of two arrays is a valuable skill. This guide delves into the intricacies of finding the Array Intersection in Java. The provided Java program not only demonstrates this process but also highlights the versatility of Java in array operations.

Understanding the Intersection of Arrays:

The intersection of two arrays represents the set of elements that are common to both arrays. This concept allows us to pinpoint shared elements, fostering a deeper understanding of array relationships.

Java Program: Finding the Intersection of Arrays

JavaProgram – Array Intersection
import java.util.Arrays;
import java.util.HashSet;

public class ArrayIntersectionFinder {
    // Method: Find the intersection of two arrays
    public static int[] findIntersection(int[] array1, int[] array2) {
        // Use HashSet to efficiently store unique elements of the first array
        HashSet<Integer> set1 = new HashSet<>();
        for (int element : array1) {
            set1.add(element);
        }

        // Use HashSet to store common elements
        HashSet<Integer> intersectionSet = new HashSet<>();

        // Iterate through the second array and add common elements to the intersection set
        for (int element : array2) {
            if (set1.contains(element)) {
                intersectionSet.add(element);
            }
        }

        // Convert the HashSet back to an array
        int[] intersectionArray = new int[intersectionSet.size()];
        int index = 0;
        for (int commonElement : intersectionSet) {
            intersectionArray[index++] = commonElement;
        }

        return intersectionArray;
    }

    // Main method: Test the intersection 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 findIntersection method
        int[] intersectionResult = findIntersection(array1, array2);

        // Print the original arrays and the intersection result
        System.out.println("Array 1: " + Arrays.toString(array1));
        System.out.println("Array 2: " + Arrays.toString(array2));
        System.out.println("Intersection of Arrays: " + Arrays.toString(intersectionResult));
    }
}

Breaking Down the Implementation:

Method findIntersection:

  • Utilizes two HashSets (set1 and intersectionSet) to efficiently store unique elements.
  • Populates set1 with elements from the first array.
  • Iterates through the second array, adding common elements to the intersectionSet.

Main Method:

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

Output:

Output – Array Intersection
Array 1: [1, 2, 3, 4, 5]
Array 2: [3, 4, 5, 6, 7]
Intersection of Arrays: [3, 4, 5]

Conclusion:

This Java program provides a robust solution for finding the intersection of two arrays. Leveraging HashSets ensures efficiency and uniqueness in the resulting intersection array. Embrace this Java technique to harmonize arrays and gain insights into shared elements. Happy coding!

Latest Posts: