# Max and Min character occurring in a string

Write a program to count the maximum and minimum occurring character in a string

#### Solution

``````using System;

namespace CharOccuerance
{
class Program
{
static void Main()
{
Console.WriteLine("Enter a string");
var charCounts = new int[str.Length];

char minChar = str[0], maxChar = str[0];
int max;

var charArray = str.ToCharArray();

for (var i = 0; i < charArray.Length; i++)
{
charCounts[i] = 1;
for (var j = i + 1; j < charArray.Length; j++)
{
if (charArray[i] != charArray[j] || charArray[i] == ' ' || charArray[i] == '0') continue;
charCounts[i]++;
charArray[j] = '0';
}
}

var min = max = charCounts[0];
for (var i = 0; i < charCounts.Length; i++)
{
if (min > charCounts[i] && charCounts[i] != '0')
{
min = charCounts[i];
minChar = charArray[i];
}

if (max >= charCounts[i]) continue;
max = charCounts[i];
maxChar = charArray[i];
}

Console.WriteLine(\$"Minimum occurring char: {minChar}");
Console.WriteLine(\$"Maximum occurring char: {maxChar}");