However, this solution is not terribly efficient. For each character in the string, we're iterating over the string again with our calls to IndexOf
, so our time complexity is \$O(n^2)\$.
In fact, we've basically just written this:
var maxCount = 0;
foreach (var c in input)
{
var count = 0;
foreach (var d in input)
{
if (c == d)
{
count++;
}
}
maxCount = Math.Max(count, maxCount);
}
return maxCount;
@BlueTrin's answer shows how you can trade space to reduce the time complexity to \$O(n)\$, so I won't go over that here.