I have this problem with my output of this code which outputs how many times a character in a string is mentioned.
class Program
{
static void Main(string[] args)
{
string str = Console.ReadLine().ToLower();
string sortedString = String.Concat(str.OrderBy(c => c));
foreach (char ch in sortedString)
{
Console.WriteLine($"{ch} => {str.Count(x => x == ch)}");
}
}
}
This is the output I get:
Alabala
a => 4
a => 4
a => 4
a => 4
b => 1
l => 2
l => 2
This is the output I want to get
Alabala
a => 4
b => 1
l => 2
Would appreciate if somebody helps me out.
You can use combination of ToDictionary()
, OrderBy()
and Distinct()
methods :
string str = "halleluyah";
var grouppedChars = str
.Distinct() // removes duplicates
.OrderBy(c => c) // orders them alphabetically
.ToDictionary( // converts to dictionary [string, int]
c => c,
c => str.Count(c2 => c2 == c));
foreach (var group in grouppedChars)
{
Console.WriteLine($"{group.Key} => {group.Value}");
}
Console.ReadKey();
Output :
a => 2
e => 1
h => 2
l => 3
u => 1
y => 1
P.S.
This is better then GroupBy()
because you don't really want to keep this chars groupped somewhere but rather keep only count of them.
Method 2, add your own struct with char information :
struct CharStatistics
{
public readonly char @char;
public readonly int count;
public CharStatistics(char @char, int count)
{
this.@char = @char;
this.count = count;
}
}
In Main method :
string str = "halleluyah";
var charsInfo = str
.OrderBy(c => c)
.Distinct()
.Select(c =>
new CharStatistics(c, str.Count(c2 => c2 == c)));
foreach (var stats in charsInfo)
{
Console.WriteLine($"{stats.@char} => {stats.count}");
}