Question: I'm working on a Kata on CodeWars in which I have to count the amount of repetitions each letter has in a string. The amount of repetitions should be stored in an int array.

The algorithm I've written seems to almost work, however I get a weird output that I can't explain. I might be missing something in the code.

```
static void Main(string[] args)
{
    string str = "abcdef";
    string input = str.ToLower();
    int count = 0;

    string[] arrayInput = Regex.Split(input, string.Empty);
    string[] alphabet = Regex.Split("abcdefghijklmnopqrstuvwxyz", string.Empty);
    int[] amounts = new int[input.Length];

    foreach (string letter in alphabet)
    {
        for (int x = 0; x < input.Length; x++)
        {
            if (arrayInput[x] == letter)
            {
               amounts[x]++;
            }
        }
    }

    foreach (int amount in amounts)
    {
        Console.Write(amount + ", ");
    }
    Console.ReadKey();
}

```

Output: 

> 
> "2, 1, 1, 1, 1, 1,"
> 
> 
> 

Expected: 

> 
> "1, 1, 1, 1, 1, 1,"
> 
> 
> 

since each letter only appears once in the string.

Answer: 
You have several issues in your code to achieve what you are looking for, like you splitted your `str` and `alphabet`by `empty string` which will give you two extra empty string in your array always! 
Anyway i think you can do this simply by using [Dictionary](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8) more efficiently like this way :

```
string str = "abcdef";
Dictionary<char, int> count_letters = new Dictionary<char, int>();
foreach (var alphabet in str)
{
    if (count_letters.ContainsKey(alphabet))
        count_letters[alphabet] ++;
    else
        count_letters.Add(alphabet, 1);
}

foreach (var result in count_letters)
    Console.WriteLine("{0} - {1}", result.Key, result.Value);

```
Your solution seems overly complicated - the combination of regular expressions, array allocations and nested loops makes it hard to see what is going on.

You can basically implement the solution as a map/reduce variant. First group the characters by their individual characters mapped to the individual characters (the mapping) and then reduce them by taking the count:

```
var input = "abcdef";
var groups = input.GroupBy(c => c);
var counts = groups.Select(g => g.Count());
Console.WriteLine(string.Join(", ", counts));

```