user3034944 user3034944 - 1 year ago 74
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
which is the count of

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());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download