# Sparse Arrays (Problem Solving: Data-Structures)

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.

Function Description

Complete the function matchingStrings in the editor below. The function must return an array of integers representing the frequency of occurrence of each query string in strings.

matchingStrings has the following parameters:

• string strings[n] – an array of strings to search
• string queries[q] – an array of query strings

Returns

• int[q]: an array of results for each query

Solution:

Javascript:

``````function matchingStrings(strings, queries) {
// Write your code here
var result=[];
let counts={};

for (const str of strings)
counts[str] = counts[str] ? counts[str] + 1 : 1;

for(var i=0;i<queries.length;i++)
if(counts.hasOwnProperty(queries[i]))
result.push(counts[queries[i]]);
else
result.push(0);

return result;
}``````

Java:

``````public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
// Write your code here
List<Integer> result = new ArrayList<Integer>();

for(int i=0;i<queries.size();i++)