Search code examples
c#stringlinqfrequency-analysis

Is it possible to create an array from the frequency of chars within a string?


Essentially, I found an old piece of LINQ C# code that counted the most frequent letter in a certain string. However, I'm using frequency analysis to solve a decoded text that has been shift-ciphered so I'm wanting it to return not just the most popular char, but a char array ordered by frequency of appearance.

Here is the LINQ code I found on here:

input.GroupBy(x => x).OrderByDescending(x => x.Count()).First().Key

Solution

  • Well you pretty much have it already.

    input.GroupBy(x => x).OrderByDescending(x => x.Count()).Select(x => x.Key).ToArray();