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.



  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8. public class Solution {
  9. public static void main(String[] args) {
  10. Scanner scan = new Scanner(System.in);
  11. int N = scan.nextInt();
  12. scan.nextLine(); // gets rid of pesky newline
  13. /* Create and fill HashMap */
  14. HashMap<String, Integer> map = new HashMap();
  15. for (int i = 0; i < N; i++) {
  16. String str = scan.nextLine();
  17. map.merge(str, 1, Integer::sum);
  18. }
  19. /* Query HashMap */
  20. int Q = scan.nextInt();
  21. scan.nextLine(); // gets rid of pesky newline
  22. for (int i = 0; i < Q; i++) {
  23. String str = scan.nextLine();
  24. System.out.println(map.getOrDefault(str, 0));
  25. }
  26. }
  27. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution


download android app

Popular HackerRank Arrays-java Solutions








codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube