Finding common scores in two sets

Report a typo

Imagine an online game where users can compete with their scores. However, to make it more challenging, the game takes two sets of scores instead of one. Your task is to write a program that initially takes two integers n, m (1 <= n, m <= 50) representing the number of scores in each set respectively. Then, it takes n positive integers representing the score set A, and m positive integers representing the score set B. The program should find the number of common scores in both sets. Each score is less than or equal to 100.

Sample Input 1:

4 5
2 4 6 2
1 4 2 1 4

Sample Output 1:

2

Sample Input 2:

3 3
3 7 5
2 5 8

Sample Output 2:

1
Write a program in Java 25
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// Take two integers n and m – the number of scores in each set
int n = scanner.nextInt();
int m = scanner.nextInt();

// Take two arrays a and b – the score sets
int[] a = new int[n];
int[] b = new int[m];

for(int i=0; i<n; i++) {
a[i] = scanner.nextInt();
}

for(int i=0; i<m; i++) {
b[i] = scanner.nextInt();
}

int commonScores = findCommonScores(a, b);

// Print out the number of common scores
System.out.println(commonScores);
}

// Declare method findCommonScores here
// This method takes two arrays as parameters and returns an integer
// This method should contain the logic to find common elements in two arrays

}

Create a free account to access the full topic