fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main(String[] args) {
  11. Scanner scanner = new Scanner(System.in);
  12. int q= scanner.nextInt();
  13. int n = scanner.nextInt();
  14.  
  15. int[] nums = new int[n];
  16. int []quer =new int[q];
  17. for (int i = 0; i < q; i++) {
  18. quer[i] = scanner.nextInt();
  19. }
  20.  
  21. for (int i = 0; i < n; i++) {
  22. nums[i] = scanner.nextInt();
  23. }
  24. List<Integer> res = countFreq(nums,quer);
  25. System.out.println(res);
  26. }
  27.  
  28. public static List<Integer> countFreq(int []nums,int []q){
  29. Map<Integer,Integer> map = new HashMap<>();
  30. List<Integer> list =new ArrayList<>();
  31. for(int num : nums){
  32. map.put(num,map.getOrDefault(num,0)+1);
  33. }
  34. for(int ele : q ){
  35. if(map.containsKey(ele)){
  36. list.add(map.get(ele));
  37. }else{
  38. list.add(0);
  39. }
  40. }
  41. return list;
  42. }
  43. }
Success #stdin #stdout 0.1s 54468KB
stdin
4 7
1 2 3 4
1 2 1 3 3 2 6
stdout
[2, 2, 2, 0]