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


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



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++)
    return result;


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++)
    return result;

Leave a Reply