simple solution for characters frequency in string object

后端 未结 4 1000
感情败类
感情败类 2021-01-27 03:13

The task what I\'m trying to do is about showing up the frequency of every single characters from the string object, for the moment I\'ve done some part of code, just doesn\'t h

4条回答
  •  生来不讨喜
    2021-01-27 03:22

    If you're looking to do it without Linq, then try

    var charDictionary = new Dictionary();
    string sign = "attitude";
    foreach(char currentChar in sign)
    {
        if(charDictionary.ContainsKey(currentChar))
        { charDictionary[currentChar]++; }
        else
        { charDictionary.Add(currentChar, 1); }
    }
    

提交回复
热议问题