       Re: Maximum in a list

• To: mathgroup at smc.vnet.net
• Subject: [mg97261] Re: [mg97209] Maximum in a list
• Date: Mon, 9 Mar 2009 01:07:28 -0500 (EST)
• References: <200903081050.FAA21521@smc.vnet.net>

```The problem can be solved in several ways; three of them follow.

Solution 1: based on Ordering and Part
Find the position of a maximizer

In:= mylist = {{1, 2}, {3, 4}, {8, 3}}; Ordering[mylist, -1,
Last[#1] <= Last[#2] & ]
Out= {2}

then get its value

In:= mylist = {{1, 2}, {3, 4}, {8, 3}}; (mylist[[#1]] & )[
Ordering[mylist, -1, Last[#1] <= Last[#2] & ][]]
Out= {3, 4}

Solution 2: based on Sort[list, crit]

In:= Sort[mylist, Last[#1] <= Last[#2] &][[-1]]
Out= {3, 4}

Solution 3: optimal (linear-time)

In:= Fold[If[Last[#1] < Last[#2], #2, #1] & , First[mylist],
Rest[mylist]]
Out= {3, 4}

2009/3/8 pacotomi <pacotomi at orange.fr>

> Hi,
>  Suppose a list
>
> mylist={{1,2},{3,4},{8,3}}
>
> I would like to extract the element {i,j} of mylist for which j is max
> (here, it is the second one {3,4}).
>
> Somebody could help me, please?
>
> Pacotomi
>

```

• Prev by Date: Re: Mathematica 7.0.1.0 and some General Comments
• Next by Date: Re: Wolfram/Alpha
• Previous by thread: Maximum in a list
• Next by thread: Re: Maximum in a list