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
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 (
set1andintersectionSet) to efficiently store unique elements. - Populates
set1with elements from the first array. - Iterates through the second array, adding common elements to the
intersectionSet.
Main Method:
- Declares example arrays (
array1andarray2) for demonstration purposes. - Calls the
findIntersectionmethod, passing both arrays. - Prints the original arrays and the result of the intersection operation.
Output:
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:
- 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



