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.
  • HackerRank Home

    HackerRank

  • |
  • Prepare
  • Certify
  • Compete
  • Hiring developers?
  1. Prepare
  2. Data Structures
  3. Arrays
  4. Sparse Arrays
  5. Discussions

Sparse Arrays

Problem
Submissions
Leaderboard
Discussions
Editorial

Sort 2180 Discussions, By:

recency

Please Login in order to post a comment

  • kh_rasikh542
    1 week ago+ 0 comments

    Here's my solution following the best possible Big O notation:

    function SparseArray(S, Q) {
    
        let ob = {}
        let result = []
        for (let i = 0; i < S.length; i++) {
            ob[S[i]] = (ob[S[i]] || 0) + 1;
        }
    
        Q.forEach(e => {
            result.push(ob[e] || 0);
        });
    
        return result;
    }
    
    0|
    Permalink
  • talhashaikh1998
    1 week ago+ 0 comments

    Simple and Clean 3 Line Solution

    public static List<Integer> matchingStrings(List<String> stringList, List<String> queries) {
        // Write your code here
            List<Integer> list = new ArrayList<>();
            for(String q : queries){
                list.add(Collections.frequency(stringList, q));
            }
            return list;
        }
    
    0|
    Permalink
  • jbrijesh97
    2 weeks ago+ 0 comments

    Python solution for O(n+m) time complexity

    def matchingStrings(stringList, queries):
        d = {}
        c = []
        for i in stringList:
            d[i] = 0
        for a in stringList:
            if a in d.keys():
                d[a] += 1
        for b in queries:
            if b in d.keys():
                c.append(d.get(b))
            else:
                c.append(0)
        return c
    
    0|
    Permalink
  • aishwaryak017
    2 weeks ago+ 0 comments

    Python solution

    def matchingStrings(stringList, queries):
        # Write your code here
        count=[]
        for i in range(len(queries)):
            count.append(stringList.count(queries[i]))
        return count      
    
    0|
    Permalink
  • rutvikLR
    2 weeks ago+ 0 comments

    Here is my javascript solution in liniear time complexity.

    function matchingStrings(stringList, queries) {
        // Write your code here
        const obj = {};
        for(let i = 0; i < stringList.length; i++) {
            obj[stringList[i]] = (obj[stringList[i]] || 0) + 1;
        }
    
        let result = []
    
        for (var i = 0; i < queries.length; i++) {
            result.push(obj[queries[i]] || 0)
        }
        return result
    }
    
    0|
    Permalink
Load more conversations

Need Help?


View editorial
View top submissions
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy