From: Robert Klemme Date: 2011-07-21T15:58:33+09:00 Subject: Re: array max value Michael Edgar wrote in post #1012048: > On Jul 21, 2011, at 2:27 AM, CC Chen wrote: > >> When I set an array k = [3,5,6,0,1,4,5,6,9,1] >> >> And I used the k.max can know the max value is 9 >> >> How do I get the number "8" (as k[8] = 9)?? > > k.index(k.max) will work. It's not completely optimal, but it's concise > and asymptotically optimal. :-) Here's another approach which is a tad more involved and iterates the array only once: irb(main):001:0> k = [3,5,6,0,1,4,5,6,9,1] => [3, 5, 6, 0, 1, 4, 5, 6, 9, 1] irb(main):002:0> k.each_with_index.max_by {|e,i| e}.last => 8 Kind regards robert -- Posted via http://www.ruby-forum.com/.