Sparse Arrays Hacker Rank Solution Best & Easiest

Sparse Arrays Problem Solution
Sparse Arrays Problem Solution

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.

strings=[‘ab’, ‘ab’, ‘abc’]
queries=[‘ab’, ‘abc’, ‘bc’]

There are 2 instances of ‘ab’, 1 of ‘abc‘ and 0 of ‘bc‘. For each query, add an element to the return array, results=[2,1,0].

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:

1. string strings[n] – an array of strings to search
2. string queries[q] – an array of query strings

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

Input Format:

The first line contains and integer n, the size of strings[].
Each of the next n lines contains a string strings[i].
The next line contains q, the size of queries[].
Each of the next q lines contains a string queries[i].

1. 1<=n<=1000
2. 1<=q<=1000
3. 1<=strings[i],queries[i]<=20.

Sparse Arrays Hacker Rank Solution

Problem solution in JavaScript programming :

function matchingStrings(strings, queries) {
    let result = [];
    for (let i = 0;i<queries.length;i++) {
            let num = 0;
            for (let j = 0;j<strings.length;j++){
                    if (strings[j] === queries [i]){
    return result;


Problem solution in Python programming :

def matchingStrings(strings, queries):
        # Write your code here
        for x in queries:
                for y in strings:
                        if x==y:
        return arr

Problem solution in C# programming :

public static List<int> matchingStrings(List<string> strings, List<string> queries)
        int count = 0;
        var res = new List<int>();
        for(int i = 0; i < queries.Count; i++){
            for(int j = 0; j < strings.Count; j++){
                if(queries[i] == strings[j]) {
            count = 0;
        return res;