给定两个数组(数组中不包含相同元素),求两个数组的交集中元素的个数(即共同出现的数,如没有则输出为None) 如输入:
5
1 2 4 6 8
6
1 2 5 6 7 8
输出: 4
import java.util.Scanner;
public class Main {
private static Scanner in;
public static void main(String[] args) {
in = new Scanner(System.in);
int a = in.nextInt();
int num = 0;
int[] array1 = new int[a];
for (int i = 0; i < a; i++) {
array1[i] = in.nextInt();
}
int b = in.nextInt();
int[] array2 = new int[b];
for (int j = 0; j < b; j++) {
array2[j] = in.nextInt();
}
in.close();
for (int k = 0; k < a; k++) {
for (int l = 0; l < b; l++) {
if (array1[k] == array2[l]) {
num += 1;
}
}
}
if (num != 0) {
System.out.println(num);
}
else{
System.out.println("None");
}
}
}