user3034944 user3034944 - 2 months ago 18
C# Question

Split List into multiple lists of similar values C#

I have a parent list something like so :-

ParentList = {a,b,c,a,c,d,b,a,c,c}


I want to split this list into smaller list something like :-

ListA = {a,a,a} ListB = {b,b} ListC= {c,c,c,c} ListD = {d}


My main intention is to get the count of the highest occurring value. In the case above it would be
4
which is the count of
ListC
.

How can I split the parent list into small list like stated in example. Or is there a way I can get the greatest count without the list splitting.

Any help is appreciated.

Answer Source

Assuming you just want the count, and not which character/string gives that count, here is a one liner (you'll need using System.Linq;)

var highestCount = ParentList.GroupBy(p => p).Max(p => p.Count());