Sparse Arrays

  • + 0 comments

    JavaScript solution:

    function matchingStrings(strings, queries) {    
        const counts = new Map();
        
        strings.forEach(string => {
            const existingCount = counts.get(string);
            if (existingCount) {
                counts.set(string, existingCount + 1);
            } else {
                counts.set(string, 1);
            }
        })
            
        return queries.map(query => counts.get(query) || 0);
    }