Search This Blog

Monday, September 29, 2025

uniqueCharacters

//*10.Check if a String Has All Unique Characters * > "abcdef" → true, "apple" → false.
string s1 = "apple";
var d1 = s1
    .GroupBy(x => x)
    .ToDictionary(x => x.Key, y => y.Count());
foreach (var item in d1)
{
    if(item.Value > 1)
    {
        Console.WriteLine("Repeating character - " + item.Key);
        return;
    }
}

WordCount

 public void wordcount()
{
    //> Given a sentence, count how many times each word appears.
    //> *(Like "This is a test. This test is simple." → \`"This=2, is=2, test=2, simple=1" *)
    string s1 = "This is a test. This test is simple.";
    var charc1 = s1.Replace(".", "").Split(" ");
    var d1 = charc1
        .GroupBy(x => x)
        .ToDictionary(x => x.Key, y => y.Count());
}

firstnonrepeatingcharacter

 public void firstnonrepeatingcharacter()
{
    // Find First Non-Repeating Character* > In a string "swiss", return the first non-repeating character → "w".
    string s1 = "swiss";
    var objdict = s1
        .GroupBy(x => x)
        .ToDictionary(x => x.Key, y => y.Count());
    foreach (var item in s1)
    {
        if (objdict[item] == 1)
        {
            Console.WriteLine("First letter is - " + item);
            return;
        }
    }
}

Sunday, September 28, 2025

Sum of Digits in a String

public void sumofdigitsinstring()
{
    string input = "s5s2re6ser38sd0f";
    // Filter out digits, convert them to int, then sum
    int sumOfDigits = input
        .Where(char.IsDigit)         // pick only digits
        .Select(c => Convert.ToInt32(c.ToString()))        // convert char to int (e.g. '3' → 3)
        .Sum();                      // sum all digits
    Console.WriteLine(  "");
}


.Select(c => Convert.ToInt32(c))  // Wont work, because it take ASCII value of char c intead of converting into char c to int values. 

Saturday, September 27, 2025

Coding Quick Tips

// break the string into array of characters (using linq )
string word = "MADAM";
var stringarr = word.Select(x => x).ToArray();
================================================================================
//convert the array of string to String
    var  str = new string(charArray);

================================================================================
What are the various Generic collections in dot net. 
>> Array, lists, dictionaries, 


Questions List

 Most frequently asked questions on Collections (15mins programming)
---
### *1. Palindrome Check*
> Write a program to check if a given string is a palindrome.
> (Classic! You already did this one.)
---
### *2. Anagram Check*
> Given two strings, check if they are anagrams (contain the same characters in any order).
csharp
"listen" → "silent" ✅
"hello" → "world" ❌

---
### *3. Find Duplicate Words in a List*
> Given a list of strings, find all duplicates and their counts using LINQ.
csharp
["cat","dog","cat","apple","dog"]  
// Output: cat=2, dog=2

---
### **4. Reverse a String Without Using Reverse()**
> Write code to reverse a string manually (loop or recursion).
---
### *5. Find Second Largest Number in an Array*
> Input: [10, 4, 6, 8, 15] → Output: 10
---
### *6. Count Vowels in a String*
> Count how many vowels appear in "programming".
> *(Can be done with Where + Count in LINQ.)*
---
### *7. Remove Duplicates from a List*
> Input: [1, 2, 2, 3, 4, 4, 5] → Output: [1, 2, 3, 4, 5]
---
### *8. Find First Non-Repeating Character*
> In a string "swiss", return the first non-repeating character → "w".
---
### *9. Word Frequency Counter*
> Given a sentence, count how many times each word appears.
> *(Like "This is a test. This test is simple." → \`"This=2, is=2, test=2, simple=1"*)
---
### *10. Check if a String Has All Unique Characters*
> "abcdef" → true, "apple" → false.
---
### *11. Rotate an Array by k Positions*
> Input: [1,2,3,4,5], k=2 → Output: [4,5,1,2,3].
---
### *12. Merge Two Sorted Arrays*
> Input: [1,3,5] and [2,4,6] → Output: [1,2,3,4,5,6].
---
### *13. Find Most Frequent Character*
> "mississippi" → 'i' with count = 4.
>> Answer: Find the character with highest frequency/Count. 

---
### *14. Sum of Digits in a String*
> "a1b2c3" → 1+2+3 = 6.
---
### *15. Check if Two Strings are Rotations*
> "abcd" and "cdab" → true.
> *(Hint: double one string "abcdabcd", then check if other is substring.)*

Sunday, September 14, 2025

Reverse String

 Q: Write code to reverse a string manually (loop or recursion) without using the 'reverse' keyword. 

==========================================================================


internal class Program
{
    static void Main(string[] args)
    {
        // Q: Write code to reverse a string manually (loop or recursion) without using the 'reverse' keyword. 

        string str = "Cycle";
        var charArray = str.ToArray();
        int totalLength = charArray.Length;
        int halflength = totalLength / 2;
        char temp;

        for (int i = 0; i < halflength; i++)
        {
            temp = charArray[i];
            charArray[i] = charArray[totalLength - 1 - i];
            charArray[totalLength - 1 - i] = temp;
        }

        //convert the array of string to String
        str = new string(charArray);

        Console.WriteLine("The reverse String is - " + str);

    }
}

Anagram

 Q: Write a program to check if two words are anagram (contains the same character in any order) or not?
------------------------------------------------------------------------------------------------------------------------------------

internal class Program
{
    static void Main(string[] args)
    {
        // Q: Write a program to check if two words are anagram (contains the same character in any order) or not?
        string s1 = "listen";
        string s2 = "silent";

        // sort the elements into array
        var arrays1 = s1.OrderBy(c => c).ToArray();
        var arrays2 = s2.OrderBy(c => c).ToArray();
        int arraylength = arrays2.Length;

        if (arrays1.Length != arrays2.Length)
        {
            Console.WriteLine("Given two words are NOT Anagram");
            return; // Get out of running method
        }

        for (int i = 0; i < arraylength-1; i++)
        {
            if (arrays1[i] != arrays2[i])
            {
                Console.WriteLine("Given two words are NOT Anagram");
                return; // Out of the running method
            }
        }

        Console.WriteLine("Given two words are Anagram");


    }
}

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);
        }

    }
}


List Playground

Friday, September 12, 2025

Array Playground

Features of Array
An array is a fixed-size collection of elements of the same type (e.g., all int, or all string)

//Define int array
var objint = new int[] { 1, 2, 3, 4, 5 };

// Define String Array
var fruits = new string[] { "Zoo", "Potato", "Banana", "Cherry", "Apple" }; // Array initialized with values

// Sorting
Array.Sort(fruits);

//Reverse
Array.Reverse(fruits);

// LinQ to Arrays
var objint2 = new int[] { 1, 5, 7, 23, 15 };
var evenNumbers = objint2.Where(x => x % 2 == 0).ToArray();
var sumNumber = objint2.Sum();
var maxNumber = objint2.Max();