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.
Computer scienceProgramming languagesJavaWorking with dataCollectionsCollection implementationsThe Map hierarchy implementations
Introduction to HashMap
Finding Intersection
Report a typo
Sample Input 1:
5
1 2 3 4 5
4 5 6 7 8Sample 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
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.