Return a collection of most frequent characters in a string My task was write a method that has input parameter - string  and this method must return a collection of symbols which are most frequently , for example: 


input: ааа bbb с
ouput: а, b
input:  ааа bb с
ouput: a
а, b, с

а b с



Here is my bad code: 

 static void test(string s)
        {
            int[] cc = new int[255];
            char c;

            for (c = (char)0; c &lt; 255; c++)
            {
                cc[c] = 0;
            }

            for (int i = 0; i &lt; s.Length; i++)
            {
                cc[s[i]]++;
            }
            int cntMax = 0;


            for ( c =(char) 0; c &lt; 255; c++)
            {
             if(cc[c] &gt; cntMax)
                {
                    cntMax = cc[c];
                }
            }
            string L = "";
            if(cntMax &gt;0 )
            {
                for ( c = (char)0; c &lt; 255; c++)
                {
                    if(cc[c] == cntMax)
                    {
                        if(c &gt; (char) 32)
                        {
                            L = L + c + " ";
                            Console.WriteLine(L);
                        }
                    }



                }
            }
        }


So  , I don't sure if understand  , I must return collection of symbols , how to do it? How can I change my code?? And also I have a question about input: if I input a b c , then my program count spaces instread of symbols , how to fix it? Sorry for my very bad code
Text and data structures

C# uses UTF-16 internally, so a char is actually 2 bytes long (and even then not all Unicode characters can be stored in a char). An array of 256 (a single byte can have 256, not 255, different values) is not sufficient. A Dictionary&lt;char, int&gt; would be more suitable here. For example, the following code is not limited to Latin characters, so feeding it some Cyrillic should work just fine:

var charCounts = new Dictionary&lt;char, int&gt;();
foreach (char c in s)
{
    if (!charCounts.ContainsKey(c))
        charCounts[c] = 1;
    else
        charCounts[c]++;
}


You said that you want to ignore space characters, but your code also ignores some control characters (but not all). You may want to use char.GetUnicodeCategory to determine which characters should be counted (there are several categories: various kinds of letters, digits, control characters and so on).

As for returning multiple characters, that's what arrays and lists are for. Instead of appending characters to a string, add them to a list and return that list when you're finished.

Other comments


Don't use 'magic values' (such as that 255 that's appearing several times). When you iterate an array, use its Length property: for (int i = 0; i &lt; cc.Length; i++).
Using a char to index an array makes your code a little confusing to read. I don't see any advantages over using a 'standard' int.
I would also not recommend reusing an indexing variable (c): it's easy to introduce bugs by forgetting to 'reset' its value.
You don't need to initialize an array of ints with 0's: C# already does that for you (0 is the default value for int).
Try using meaningful variable names: text or input instead of s, charCounts or even characterCounts instead of cc, and so on. It'll make your code easier to understand, and that's a good thing when you have to revisit code a few months later.
The easiest way to get the maximum value from a collection is to use Linq's Max method: cc.Max(). There are also several other Linq methods that could make this method easier to write, such as GroupBy, Where and  ToDictionary.