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:
- Input Arrays: Two integer arrays whose union and intersection we want to compute.
- HashSet for Union: A
HashSet
is used to store all unique elements from both arrays to compute the union. - HashSet for Intersection: A separate
HashSet
is used to find common elements between the two arrays. - 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
- Copy the code into a file named
ArrayUnionIntersection.java
. - Open a terminal and navigate to the directory containing the file.
- Compile the program using the command:
javac ArrayUnionIntersection.java
. - Run the compiled program with:
java ArrayUnionIntersection
. - 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 newHashSet
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.