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

    }
}