Introduction
The intersection of two arrays refers to the elements that are present in both arrays. This task is common in various applications where you need to find common elements between datasets. This guide will walk you through writing a Java program that finds the intersection of two arrays.
Problem Statement
Create a Java program that:
- Prompts the user to enter the sizes and elements of two arrays.
- Finds and displays the intersection of the two arrays.
Example:
- Input:
- Array 1:
[1, 2, 3, 4, 5] - Array 2:
[4, 5, 6, 7, 8]
- Array 1:
- Output:
"Intersection: [4, 5]"
Solution Steps
- Read the Sizes and Elements of the Two Arrays: Use the
Scannerclass to take the sizes and elements of the two arrays as input from the user. - Use a Data Structure to Track Elements: Utilize a
HashSetto store the elements of the first array and then check for common elements in the second array. - Find the Intersection: Compare the elements of the second array against the
HashSetto find common elements. - Display the Result: Print the intersection of the two arrays.
Java Program
// Java Program to Find the Intersection of Two Arrays
// Author: https://www.rameshfadatare.com/
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ArrayIntersection {
public static void main(String[] args) {
// Step 1: Read the size and elements of the first array from the user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the first array: ");
int size1 = scanner.nextInt();
int[] array1 = new int[size1];
System.out.println("Enter the elements of the first array:");
for (int i = 0; i < size1; i++) {
array1[i] = scanner.nextInt();
}
// Step 1: Read the size and elements of the second array from the user
System.out.print("Enter the size of the second array: ");
int size2 = scanner.nextInt();
int[] array2 = new int[size2];
System.out.println("Enter the elements of the second array:");
for (int i = 0; i < size2; i++) {
array2[i] = scanner.nextInt();
}
// Step 2: Find the intersection of the two arrays
int[] intersectionArray = findIntersection(array1, array2);
// Step 3: Display the intersection
System.out.println("Intersection of the two arrays:");
for (int element : intersectionArray) {
System.out.print(element + " ");
}
}
// Method to find the intersection of two arrays
public static int[] findIntersection(int[] array1, int[] array2) {
Set<Integer> set1 = new HashSet<>();
Set<Integer> intersectionSet = new HashSet<>();
// Add all elements of the first array to set1
for (int element : array1) {
set1.add(element);
}
// Find common elements between array1 and array2
for (int element : array2) {
if (set1.contains(element)) {
intersectionSet.add(element);
}
}
// Convert intersection set to an array
int[] intersectionArray = new int[intersectionSet.size()];
int index = 0;
for (int element : intersectionSet) {
intersectionArray[index++] = element;
}
return intersectionArray;
}
}
Explanation
Step 1: Read the Sizes and Elements of the Two Arrays
- The
Scannerclass is used to read the sizes of the two arrays and their elements. ThenextInt()method captures the sizes and each element.
Step 2: Find the Intersection of the Two Arrays
- The
findIntersection()method is used to find the intersection of the two arrays:- A
HashSetnamedset1is used to store all elements from the first array (array1). This allows for efficient lookups to check if an element from the second array exists in the first array. - Another
HashSetnamedintersectionSetis used to store the common elements found in both arrays. - The method then converts the
intersectionSetto an array, which is returned to the caller.
- A
Step 3: Display the Intersection
- The program prints the elements of the intersection array using a
forloop.
Output Example
Example 1:
Enter the size of the first array: 5
Enter the elements of the first array:
1 2 3 4 5
Enter the size of the second array: 5
Enter the elements of the second array:
4 5 6 7 8
Intersection of the two arrays:
4 5
Example 2:
Enter the size of the first array: 4
Enter the elements of the first array:
10 20 30 40
Enter the size of the second array: 3
Enter the elements of the second array:
20 50 60
Intersection of the two arrays:
20
Example 3:
Enter the size of the first array: 6
Enter the elements of the first array:
2 4 6 8 10 12
Enter the size of the second array: 5
Enter the elements of the second array:
1 3 5 7 9
Intersection of the two arrays:
(In this case, there is no intersection, so no elements are printed.)
Conclusion
This Java program demonstrates how to find the intersection of two arrays using HashSet. The program efficiently finds common elements by leveraging the HashSet‘s ability to store unique elements and perform quick lookups. This exercise is valuable for understanding how to work with sets and arrays in Java programming.