Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

I sometimes use a little function MaxByMaxBy, 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]

I sometimes use a little function 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]

I sometimes use a little function 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]
deleted 18 characters in body
Source Link

I sometimes use a little function MaxByMaxBy, 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@Brett's and @RM's@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 the concept of 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 maximumsmaxima:

allMaxBy[data_, fun_] := Last@SplitBy[SortBy[data, fun], fun]

I sometimes use a little function 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 based on the same principle as @Brett's solution, but it is slower. @Brett and @RM'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 the concept of 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 maximums:

allMaxBy[data_, fun_] := Last@SplitBy[SortBy[data, fun], fun]

I sometimes use a little function 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]
Source Link
Szabolcs
  • 236.5k
  • 31
  • 641
  • 1.3k

I sometimes use a little function 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 based on the same principle as @Brett's solution, but it is slower. @Brett and @RM'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 the concept of 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 maximums:

allMaxBy[data_, fun_] := Last@SplitBy[SortBy[data, fun], fun]