Created
October 16, 2020 02:51
-
-
Save benaadams/17c2d4376338b75309c4d919adcb2714 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int Entropy(this string s) | |
{ | |
if (string.IsNullOrEmpty(s)) return 0; | |
var array = ArrayPool<char>.Shared.Rent(s.Length); | |
s.CopyTo(0, array, 0, s.Length); | |
var span = array.AsSpan(0, s.Length); | |
span.Sort(); | |
var last = -1; | |
var count = 0; | |
foreach (var c in span) | |
{ | |
if (c == last) continue; | |
last = c; | |
count++; | |
} | |
ArrayPool<char>.Shared.Return(array); | |
return count; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment