Maximum number of occurrences a character appears in an array of strings

前端 未结 3 519
刺人心
刺人心 2021-01-20 01:31

In C#, given the array :

string[] myStrings = new string[] {
  \"test#test\",
  \"##test\",
  \"######\", // Winner (outputs 6)
};

How can

3条回答
  •  [愿得一人]
    2021-01-20 02:18

    You can write something like this. Note the usage of DefaultIfEmpty, to not throw an exception if myStrings is empty, but revert to 0.

    var maximum = myStrings.Select(e => e.Count(ee => ee == '#')).DefaultIfEmpty().Max();
    

提交回复
热议问题