How to find out the duplicate characters in a string?

时间:2015-06-26 10:07:17

标签: c# asp.net

I take a string with integer count

int count=0;
string s="wow"

and i am using foreach loop to count number of characters in a specified string

foreach(char ch in s)
{

count++
}

so how can i count those characters which are repeated in my string like 'w'.

2 个答案:

答案 0 :(得分:3)

Do like this

  string s="wow";
  int repeats= s.Length - s.ToCharArray().Distinct().Count();

答案 1 :(得分:3)

Try this )

string test = "aababc";
        var result = test.GroupBy(c => c).Where(c => c.Count() > 1).Select(c => new { charName = c.Key, charCount = c.Count()});
相关问题