The following code
var badChars = new[] { '~', '>', '|' };
var strings = new[]
{
"I ~ love >> cookies |||",
"I >>>> love pizza ~",
"I hate tildas~~"
};
var grouped = from str in strings
let count = str.Count(c => badChars.Contains(c))
orderby count ascending
select new { Text = str, Count = count };
var tuple = grouped.First();
Console.WriteLine("Text is '{0}' with {1} occurences", tuple.Text, tuple.Count);
should do the thing for a single minimum. However, since you possibly need multiple minimums, the following one should help:
var badChars = new[] { '~', '>', '|' };
var strings = new[]
{
"I ~ love >> cookies |||",
"I >>>> love pizza ~",
"I hate tildas~~",
"Bars are also | | bad"
};
var grouped = (from str in strings
let count = str.Count(c => badChars.Contains(c))
orderby count ascending
select new { Text = str, Count = count }).ToList();
int min = grouped[0].Count;
var minimums = grouped.TakeWhile(g => g.Count == min);
foreach(var g in minimums)
Console.WriteLine("Text is '{0}' with {1} occurences", g.Text, g.Count);