直接使用Hash表进行记录即可

import java.util.*;


public class Main{
    
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        sc.nextLine();
        String line = sc.nextLine();
        String[] candidates = line.split(" ");
        Map<String,Integer> votes = new HashMap<>();
        for(String candidate:candidates){
            votes.put(candidate, 0);
        }

        int numOfVote = sc.nextInt();
        int invalid = 0;
        sc.nextLine();
        String[] vs = sc.nextLine().split(" ");
        for(String v:vs){
            if(votes.containsKey(v)){
                votes.put(v, votes.get(v) + 1);
            }else{
                invalid++;
            }
        }
        for(String candidate:candidates){
            System.out.println(candidate + " : " + votes.get(candidate));
        }
        System.out.println("Invalid : " + invalid);
    }
    
}