Sparse Arrays

Sort by

recency

|

400 Discussions

|

  • + 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);
    }
    
  • + 0 comments

    C# Code

    public static List matchingStrings(List strings, List queries) { var resultList = new List();

        foreach(string queryItem in queries)
        {
            resultList.Add(strings.Count(e => e == queryItem));
        }
    
        return resultList;
    }
    
  • + 0 comments

    Java solution using HashMap:

        public static List<Integer> matchingStrings(List<String> strings, List<String> queries) {
    
            Map<String, Integer> countMap = new HashMap<>();
    
            for(String query : queries){
                countMap.put(query, 0);
            }
    
            for(String str : strings){
                if(countMap.containsKey(str)){
                    Integer count = countMap.get(str);
                    countMap.put(str, count + 1);
                }
            }
    
            List<Integer> returnList = new ArrayList<>();
            for(String query : queries){
                returnList.add(countMap.get(query));
            }
    
            return returnList;
        }
    
  • + 0 comments

    Java:

    public static List matchingStrings(List strings, List queries) { // Write your code here

    List result = new ArrayList<>();

    for (String string : queries) {

    int counter = 0;
    
    for (String seek : strings) {
    
        if(seek.equals(string)){
            counter ++;
        }
    
    }
    
    result.add(counter);
    

    }

    return result;
    
    
    }
    

    }

  • + 0 comments
    fun matchingStrings(strings: Array<String>, queries: Array<String>): Array<Int> {
            val resultArray = Array(queries.size){0}
            queries.forEachIndexed { index, element ->
                resultArray[index] = strings.count { it == element }
            }
            return resultArray
    }