Skip to content
Search lessons, topics, tests…
Esc

    ↑ ↓ moveEnter openEsc close

    Coding problem

    Count each character

    EasyAbout 15 minFreeLearn it: Find a value by name with a dictionary

    The task

    A password checker shows how often each character appears, in the order the characters first appear, so that users can spot repeats.

    Complete CountCharacters(string text). Return a list of (Character, Count) pairs, one pair for each different character, counting every occurrence. Put the pairs in the order in which their characters first appear, not in alphabetical order. Upper-case and lower-case letters are different characters. An empty text gives an empty list. The text is never null and holds only English letters.

    Signature

    public static List<(char Character, int Count)> CountCharacters(string text)

    Examples

    • "banana" → [('b', 1), ('a', 3), ('n', 2)]. b appears first, then a, then n.
    • "aAa" → [('a', 2), ('A', 1)]. 'A' is a different character from 'a'.

    Limits: Up to 100,000 characters.

    The tests check: an empty text, one character, a character repeated, first-seen order (not alphabetical), occurrences far apart, letter case, an order that does not follow the counts, characters that first appear late, and a 100,000-character text.

    Hints

      The solution opens by itself when all tests pass. You remember more if you try first.

      Sign in to see the solution

      Your code

      C#Solution.csLoading the editor…

      Already included: using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net.Http; using System.Threading; using System.Threading.Tasks;

      public static class Solution
      {
          // Returns each character and its count, in first-seen order.
          public static List<(char Character, int Count)> CountCharacters(string text)
          {
              throw new NotImplementedException();
          }
      }

      Sign in to run and submit your code. It stays here while you do.