Java
Java

 

 

This document contains a Java program to find the union and intersection of two arrays using hashing techniques. We will use a HashSet to store elements and achieve efficient lookups.

Program Explanation

The program consists of the following main parts:

  1. Input Arrays: Two integer arrays whose union and intersection we want to compute.
  2. HashSet for Union: A HashSet is used to store all unique elements from both arrays to compute the union.
  3. HashSet for Intersection: A separate HashSet is used to find common elements between the two arrays.
  4. Output: The program prints both the union and intersection of the arrays.

Java Program


import java.util.HashSet;

public class ArrayUnionIntersection {

    /**
     * This method computes the union of two integer arrays.
     *
     * @param array1 First array of integers.
     * @param array2 Second array of integers.
     * @return A HashSet containing the union of the two arrays.
     */
    public static HashSet union(int[] array1, int[] array2) {
        HashSet unionSet = new HashSet<>();
        
        // Add elements from the first array
        for (int num : array1) {
            unionSet.add(num);
        }
        
        // Add elements from the second array
        for (int num : array2) {
            unionSet.add(num);
        }
        
        return unionSet;
    }

    /**
     * This method computes the intersection of two integer arrays.
     *
     * @param array1 First array of integers.
     * @param array2 Second array of integers.
     * @return A HashSet containing the intersection of the two arrays.
     */
    public static HashSet intersection(int[] array1, int[] array2) {
        HashSet intersectionSet = new HashSet<>();
        HashSet set1 = new HashSet<>();
        
        // Add elements from the first array to the set
        for (int num : array1) {
            set1.add(num);
        }
        
        // Check elements from the second array
        for (int num : array2) {
            if (set1.contains(num)) {
                intersectionSet.add(num);
            }
        }
        
        return intersectionSet;
    }

    public static void main(String[] args) {
        int[] array1 = {1, 2, 3, 4, 5};
        int[] array2 = {4, 5, 6, 7, 8};

        HashSet unionResult = union(array1, array2);
        HashSet intersectionResult = intersection(array1, array2);

        System.out.println("Union of the two arrays: " + unionResult);
        System.out.println("Intersection of the two arrays: " + intersectionResult);
    }
}

How to Run the Program

  1. Copy the code into a file named ArrayUnionIntersection.java.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using the command: javac ArrayUnionIntersection.java.
  4. Run the compiled program with: java ArrayUnionIntersection.
  5. The output will display the union and intersection of the two arrays.

Output Example

When the program is executed, it will produce output similar to the following:

Union of the two arrays: [1, 2, 3, 4, 5, 6, 7, 8]
Intersection of the two arrays: [4, 5]

 

Explanation of the Code:

  • HashSet: We use HashSet for its O(1) average time complexity for insert and lookup operations, making our union and intersection calculations efficient.
  • Union Method: The union method adds all elements from both arrays to a single HashSet, ensuring all elements are unique.
  • Intersection Method: The intersection method checks for elements in the second array that exist in the first HashSet, adding them to a new HashSet for the intersection.
  • Main Method: This is where the arrays are defined, and the methods are called to compute and print the union and intersection.

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)