简体   繁体   中英

Get the most common (frequent) string entry in array

I have a string array with values in it (duh...).

Is there an easy way of getting the entry which occurs the most? Something like

values[37].getMostOften();

Cheers :)

You can use GroupBy :

var mostCommonValue = values.GroupBy(v => v)
                            .OrderByDescending(g => g.Count())
                            .Select(g => g.Key)
                            .FirstOrDefault();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM