fork download
  1. import java.util.*;
  2.  
  3. class Ideone {
  4. public static void main(String[] args) throws java.lang.Exception {
  5. Scanner sc = new Scanner(System.in);
  6. HashMap<Integer, Integer> map = new HashMap<>();
  7.  
  8. int n = sc.nextInt();
  9. int[] arr = new int[n];
  10.  
  11. // Read array and populate the map
  12. for (int i = 0; i < n; i++) {
  13. arr[i] = sc.nextInt();
  14. map.put(arr[i], map.getOrDefault(arr[i], 0) + 1);
  15. }
  16.  
  17. // Process queries
  18. int q = sc.nextInt();
  19. for (int i = 0; i < q; i++) {
  20. int query = sc.nextInt();
  21. int count = map.getOrDefault(query, 0);
  22. System.out.println("count of query " + query + " is " + count);
  23. }
  24. }
  25. }
  26.  
Success #stdin #stdout 0.17s 58912KB
stdin
5 
1 1 1 2 2
3
1
2
3
stdout
count of query 1 is 3
count of query 2 is 2
count of query 3 is 0