Given a List<string> with the following elements: 1, 2, 2, 1, 3, 3, 5, ?, , 4, A, 1, 7, 1, 9, 3
What is a good way to find the number that occurs the most, that excludes null values and non-numerics? LINQ is okay (I know it’s not always very efficient, but it is fun. 🙂 ).
I’d use something like this:
Note that instead of
OrderByDescendingyou could use MoreLINQ‘sMaxByoperator:Either way, the result will have the value and the number of times it occurred.
(Also note that this will include numbers which are too big to store in an
int– if you want to perform actual parsing, you should do that instead.)