public static char? FindMostOccuranceOfCharInString(string value)
{
char? maxOccuranceChar = null;
int maxOccuranceValue = 0;
if (string.IsNullOrWhiteSpace(value))
return null;
char[] arr = value.ToLower().Trim().ToCharArray();
Dictionary<char, int> _dic = new Dictionary<char, int>();
for (int i = 0; i < arr.Length; i++)
{
if (arr[i] != ' ')
{
if (!_dic.ContainsKey(arr[i]))
{ _dic.Add(arr[i], 1);}
else
{ _dic[arr[i]]++;
}}}
foreach (KeyValuePair<char, int> item in _dic)
{
if (item.Value > maxOccuranceValue)
{
maxOccuranceChar = item.Key;
maxOccuranceValue = item.Value;
} }
return maxOccuranceChar;
}
// Find second largest char
ReplyDeletestring str = "cacbcdcecdddddddd";
int maxCount = 0;
int index = 0;
var charStr = str.ToCharArray();
int n = charStr.Length;
for (int i = 0; i < n; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
if (charStr[i] == charStr[j])
{
count++;
}
}
if (count > maxCount)
{
maxCount = count;
index = i;
}
}
Console.WriteLine($"Max Count : {maxCount}, CharVal : {charStr[index]}");