If you have not studied dictionaries yet, recall that
char
values can be used as an array index (it's simply a number between 0 and char.MaxValue). If you create an array of counters, you'd be able to run through the characters in your string with a
foreach
loop, incrementing your counters along the way. Then you can print your result by going through the counters, and printing non-zero values (character code equals the index of the counter).
P.S. If you just need an answer, try this:
var res = str.Distinct().ToDictionary(c => c, c => str.Where(a => a == c).Count());
It's a correct solution, but don't be surprised if your teacher suspects something ;)