I sometimes use a little function MaxBy
MaxBy
, made to be analogous with SortBy
:
MaxBy[list_, fun_] := list[[First@Ordering[fun /@ list, -1]]]
You need the largest element by length, so you can evaluate
MaxBy[data, Length]
Note: this is based on the same principle as @Brett's solution, but it is slower. @Brett's and @R.M's exploit the fact that Mathematica sorts by length by default, while my solution explicitly uses Length
. I still think it's a useful little function, so I shared it again.
The problem with MaxBy
is that it only returns a single element, while there may be more than one list of the same length. Here's a somewhat slow but simple implementation that returns all maxima:
allMaxBy[data_, fun_] := Last@SplitBy[SortBy[data, fun], fun]