In: Computer Science
Please use Java language to write an efficient function that compute the intersection of two arrays of integers (not necessary sorted).
CODE
import java.util.HashSet;
public class Main
{
public static void
findIntersectionForUnsortedArrays(int arr1[], int arr2[])
{
HashSet<Integer>
hs = new HashSet<>();
for (int i = 0; i <
arr1.length; i++)
hs.add(arr1[i]);
for (int i = 0; i <
arr2.length; i++)
if (hs.contains(arr2[i]))
System.out.print(arr2[i] + " ");
}
public static void main(String args[])
{
int arr1[] = {1, 9, 5, 4, 8,
6};
int arr2[] = {7, 8, 9,
1, 17, 20, 4};
findIntersectionForUnsortedArrays(arr1, arr2);
}
}