# How to find row maximum value? [duplicate]

Suppose if I have following list:

   list=
{
{0.0,10},
{0.1,10},
{0.2,10},
{0.2,55.3},
{0.3,10},
{0.3,54},
{0.4,10},
{0.5,10},
{0.5,45.5}
};


And I want result:

{
{0.1, 10},
{0.2, 55.3},
{0.3, 54},
{0.4, 10},
{0.5, 45.5}
}


I tried:

test[{next_}] := Module[{}, column = Cases[list, {next, _}];
Cases[column, {_, Max[column[[All, 2]]]}]];
Flatten[Table[test[{i}], {i, 0.1, 0.5, 0.1}], 1]


and this works. But with large lists works slow. How get faster results?

-
(SortBy[#, Last] & /@ GatherBy[list, First])[[ All, -1]] but I think it is a duplicate –  Kuba Feb 12 at 11:42
@kuba, I believe so, but have not found it... here it is...mathematica.stackexchange.com/questions/900/… –  rasher Feb 12 at 11:46
Closely related the only difference is that you need to change Total to Max. –  Kuba Feb 12 at 11:52
As comment v answer, and being uncertain of arbitration 'duplicate' or not: Last@Reap[Sow @@@ (Reverse[#] & /@ list), _, {#1, Max@#2} &] –  ubpdqn Feb 12 at 12:02
@Kuba Sorry, my bad. :/ –  Pickett Feb 12 at 13:25
show 2 more comments

## marked as duplicate by rm -rf♦Feb 13 at 16:15

Reap[Sow[#[[2]], #[[1]]] & /@ list, _, {#1, Max[#2]} &][[2]]

{{0., 10}, {0.1, 10}, {0.2, 55.3}, {0.3, 54}, {0.4, 10}, {0.5, 45.5}}

-

Another way is to gather the elements with identical first values together, then apply the Max function:

list = {{0.0, 10}, {0.1, 10}, {0.2, 10}, {0.2, 55.3}, {0.3, 10}, {0.3, 54},
{0.4, 10}, {0.5, 10}, {0.5, 45.5}};
g = Gather[list, #1[[1]] == #2[[1]] &];
Transpose[{Union[First@Transpose[list]], Map[Max, g, 2]}]
{{0., 10}, {0.1, 10}, {0.2, 55.3}, {0.3, 54}, {0.4, 10}, {0.5, 45.5}}


As Kuba points out, it's a bit shorter (and probably clearer) to use

g = GatherBy[list, First]


I'm not sure if faster, but shorter for sure GatherBy[list, First] :) –  Kuba Feb 12 at 13:26