Search This Blog

Saturday, September 13, 2025

Count

Q1:  Consider a list of strings values that repeated words. Find the 2nd height count of word in list.
Q2: Find Second Largest Number in an Array. Input: `[10, 4, 6, 8, 15]` → Output: "10"
Q3: Count Vowels in a String. Count how many vowels appear in `"programming"`. Can be done with `Where` + `Count` in LINQ.

============================================================================
Q1:  Consider a list of strings values that repeated words. Find the 2nd height count of word in list.

Answer:
internal class Program
{
    static void Main(string[] args)
    {
        // Q: Consider a list of strings values that repeated words. Find the 2nd height count of word in list.
        var objstr = new List<string>() { "ram", "apple", "cat", "cat", "cat", "apple" };

        // write a linq/ lambda to get word, count and save it in a dictionary. 
        var wordCountDict = objstr
               .GroupBy(x => x)
               .ToDictionary(g => g.Key, l => l.Count());

        // 
        var secondhighest = wordCountDict
            .OrderByDescending(x => x.Value) // sort the list by count. 
            .Skip(1) // Skip the first element to get the 2nd elemetn. 
            .FirstOrDefault();

        Console.WriteLine("The 2nd Highest string is - " + secondhighest.Key + " and its count is -- " + secondhighest.Value);

    }
}
=============================================================================
Q2: Find Second Largest Number in an Array. Input: `[10, 4, 6, 8, 15]` → Output: "10"

Answer:

internal class Program
{
    static void Main(string[] args)
    {
        // Q: Find Second Largest Number in an Array. Input: `[10, 4, 6, 8, 15]` → Output: "10"
        var arrayNum = new int[] { 10, 4, 6, 8, 15 };

        // Sort the list and get second highest
        var secondHighest = arrayNum
            .DistinctBy(x=>x)
            .OrderByDescending(c => c)
            .Skip(1)
            .FirstOrDefault();;

        Console.WriteLine("The second highest number in given array is - " + secondHighest);

    }
}
=============================================================================
Q3: Count Vowels in a String. Count how many vowels appear in `"programming"`. Can be done with `Where` + `Count` in LINQ.

Answer:

internal class Program
{
    static void Main(string[] args)
    {
        // Q: Count Vowels in a String. Count how many vowels appear in `"programming"`. Can be done with `Where` + `Count` in LINQ.

        string word = "programmingappleleopard";

        //define a array of vowels
        var vowels = new char[] { 'a', 'e', 'i', 'o', 'u' };

        // Write a lambda/linq query to get count of characters and store it in Dictionary
        var vowelsCount = word
            .Where(x => vowels.Contains(x))
            .GroupBy(x => x)
            .ToDictionary(x => x.Key, x => x.Count());

        foreach (var item in vowelsCount)
        {
            Console.WriteLine(item.Key + ", " + item.Value);
        }

    }
}


No comments:

Post a Comment