Sparse Arrays

  • + 0 comments

    C# Solution

     List<int> countArr = new List<int>();
        //distinct check not required
        //List<string> distQuery = queries.Distinct().ToList();
        var frequency = strings.GroupBy(x=>x).ToDictionary(x => x.Key, x => x.Count());
        foreach(var q in queries)
        {
            if(frequency.ContainsKey(q))
            {
            Console.WriteLine("{0} element has count of {1} ",q, frequency[q]);
            countArr.Add(frequency[q]);
            }
            else
            {
                countArr.Add(0);
            }
        }
        return countArr;