Sitemap
He Codes IT

He Codes IT

A tech blog for Computer Science Students, offering posts on courses like Databases, Data Structures, Algorithms and Data Science.

Sparse arrays hackerrank solution JavaScript and Python

Problem Sparse arrays hackerrank

3 min readJan 24, 2023

--

In Sparse Arrays HackerRank Problem Both a collection of query strings and a collection of input strings are present. Find the number of times each query string appears in the list of input strings. Return the results as an array. The Original Link of HackerRank Problem is Here : Link.

Example

StringList = ['ab','ab','abc']
queries = ['ab','abc','a']
Return
[2,1,0]

Description of Function

Fill up the matchingStrings function in the editor below. An array of numbers reflecting the frequency of occurrence of each query string in stringList must be returned by the function. The following are the arguments for matchingStrings:

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

Returns

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

Sparse arrays hackerrank solution JavaScript

Firstly declare a list to store the returned list.

function matchingStrings(stringList, queries) {
// Write your code here
let return_list = [];
}

Secondly, Declare an outer loop which loops through the queries list.

function matchingStrings(stringList, queries) {
// Write your code here
let return_list = [];
for (let i = 0; i < queries.length; i++){

}
}

Write another loop which will iterate through the values present in stringList List. Declare a counter too, to count the number of occurrences queries element present inside the string List.

function matchingStrings(stringList, queries) {
// Write your code here
let return_list = [];
for (let i = 0; i < queries.length; i++){
let counter= 0
for (let j =0 ; j < stringList.length; j++){
}
}
}
}

Check if Element i of Queries List is equal to the element j of stringList. Remember that one element of Queries List is being compared with all elements of StringList. It means that if queries List has an element which is appearing two times inside string list then it will counter it two times. Make sure to increment the counter variable.

function matchingStrings(stringList, queries) {
// Write your code here
let return_list = [];
for (let i = 0; i < queries.length; i++){
let counter= 0
for (let j =0 ; j < stringList.length; j++){
if (queries[i] == stringList[j]){
counter +=1
}
}
}
}

Finally, Push the Counter variable inside your returned List which you declared earlier and return that list outside of the loop.

Get He Codes IT’s stories in your inbox

Join Medium for free to get updates from this writer.

Solution

function matchingStrings(stringList, queries) {
// Write your code here
let return_list = [];
for (let i = 0; i < queries.length; i++){
let counter= 0
for (let j =0 ; j < stringList.length; j++){
if (queries[i] == stringList[j]){
counter +=1
}
}
return_list.push(counter)
}
return return_list
}

Congratulations:

Sparse arrays hackerrank solution Python

The solution to this problem in Python is far easier than javascript. Because Python has already a built in function named as count which count’s the number of occurrence of Particular variable inside a list. You just have to iterate over the values in queries and check through .count function that particular value exist in stringList or not.

Solution

def matchingStrings(stringList, queries):
returnList = []
for values in queries:
returnList.append(stringList.count(values))
return returnList

READ MORE

Data Structures related posts visit HERE

Python-related posts Visit HERE

C/C++ related posts Visit HERE

Databases related posts Visit HERE

Algorithms related posts visit HERE

Data Science related posts visit HERE

Download Games like ZD22

--

--

He Codes IT
He Codes IT

A tech blog for Computer Science Students, offering posts on courses like Databases, Data Structures, Algorithms and Data Science.