Skip to content

Instantly share code, notes, and snippets.

@Retterath
Created November 7, 2018 09:22
Show Gist options
  • Save Retterath/222186f0216e2b0f6c2271bfdb09ac58 to your computer and use it in GitHub Desktop.
Save Retterath/222186f0216e2b0f6c2271bfdb09ac58 to your computer and use it in GitHub Desktop.
Count Chars in a String
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CharInString
{
class Program
{
public static void Main()
{
SortedDictionary<char, int> text = new SortedDictionary<char, int>();
char[] characters = Console.ReadLine()
.Where(x => !Char.IsWhiteSpace(x))
.ToArray();
foreach (var character in characters)
{
if (text.ContainsKey(character))
{
text[character]++;
}
else
{
text.Add(character, 1);
}
}
foreach (var character in text.OrderByDescending(x => x.Value))
{
Console.WriteLine($"{character.Key} -> {character.Value}");
}
}
//var text = new SortedDictionary<char, int>();
//var inputText = Console.ReadLine();
// foreach (var character in inputText)
// {
// if (character == ' ')
// {
// continue;
// }
// if (text.ContainsKey(character))
// {
// text[character]++;
// }
// else
// {
// text.Add(character, 1);
// }
// }
// foreach (var character in text.OrderByDescending(x => x.Value))
// {
// Console.WriteLine($"{character.Key} -> {character.Value}");
// }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment