15 August 2017

firstNotRepeatingCharacter

My solution:
char firstNotRepeatingCharacter(string s) 
{
    var d = new Dictionary<char, int>();
    foreach (var c in s)
        if (!d.ContainsKey(c))
            d[c] = 1;
        else
            d[c]++;

    return d.Any(pair => pair.Value == 1) ? d.Where(pair => pair.Value == 1).Select(pair => pair.Key).First() : '_';
}

Previous Next