Finding Intersection

Report a typo

Write a program that accepts two arrays of integers of length n and uses a HashMap to find the common elements in both arrays. The program must print out the intersection of the two arrays in ascending order and print nothing if no common values are found.

Sample Input 1:

5
1 2 3 4 5
4 5 6 7 8

Sample Output 1:

4 5 
Write a program in Java 17
import java.util.HashMap;
import java.util.Scanner;

class Main {
private static void printCommon(int[] firstArray, int[] secondArray) {
// Enter your Code Here
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] firstArray = new int [n];
int[] secondArray = new int [n];
for (int i = 0; i < n; ++i) {
firstArray[i] = scanner.nextInt();
}
for (int i = 0; i < n; ++i) {
secondArray[i] = scanner.nextInt();
}

printCommon(firstArray,secondArray);
}
}
___

Create a free account to access the full topic