HackerRank Java Arrays- Sparse Arrays
There is a collection of input strings and a collection of query strings. For each query string, determine how many times it occurs in the list of input strings. Return an array of the results.
- import java.io.*;
- import java.math.*;
- import java.security.*;
- import java.text.*;
- import java.util.*;
- import java.util.concurrent.*;
- import java.util.regex.*;
-
- public class Solution {
-
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- int N = scan.nextInt();
- scan.nextLine(); // gets rid of pesky newline
-
- /* Create and fill HashMap */
- HashMap<String, Integer> map = new HashMap();
- for (int i = 0; i < N; i++) {
- String str = scan.nextLine();
- map.merge(str, 1, Integer::sum);
- }
-
- /* Query HashMap */
- int Q = scan.nextInt();
- scan.nextLine(); // gets rid of pesky newline
- for (int i = 0; i < Q; i++) {
- String str = scan.nextLine();
- System.out.println(map.getOrDefault(str, 0));
- }
- }
- }