Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,974 answers

573 users

How to count occurrences of each letter in a char array with C#

1 Answer

0 votes
using System;
using System.Collections.Generic;

class CountLettersInCharArray
{
    static void Main()
    {
        char[] charArray = { 'C', '#', 'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g' }; 
        Dictionary<char, int> letterCount = new Dictionary<char, int>();

        foreach (char c in charArray) {
            if (letterCount.ContainsKey(c)) {
                letterCount[c]++;
            }
            else {
                letterCount[c] = 1;
            }
        }

        foreach (var item in letterCount) {
            Console.WriteLine($"Letter {item.Key}: {item.Value} times");
        }
    }
}

 
 
/*
run:
     
Letter C: 1 times
Letter #: 1 times
Letter p: 1 times
Letter r: 2 times
Letter o: 1 times
Letter g: 2 times
Letter a: 1 times
Letter m: 2 times
Letter i: 1 times
Letter n: 1 times
     
*/
 

 



answered Mar 2, 2025 by avibootz
...