We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Java using a map to save the frequencies. This solution iterates each list only once.
publicstaticList<Integer>matchingStrings(List<String>strings,List<String>queries){// Write your code hereHashMap<String,Integer>stringsMap=newHashMap<>();for(Stringstring:strings){if(stringsMap.containsKey(string)){stringsMap.put(string,stringsMap.get(string)+1);}else{stringsMap.put(string,1);}}ArrayList<Integer>results=newArrayList<>(queries.size());for(Stringquery:queries){Integerfrequency=stringsMap.get(query);results.add(frequency==null?0:frequency);}returnresults;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Sparse Arrays
You are viewing a single comment's thread. Return to all comments →
Java using a map to save the frequencies. This solution iterates each list only once.