Key Associated w/ Maximum Value in Hash

T

Timothy Baron

Simple question: what's the cleanest way to retrieve a key associated
with the maximum value of a hash. For instance, how do I retrieve 'b'
from the following example:

hash = Hash.new
hash['a']=4
hash['b']=10
hash['c']=7
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Simple question: what's the cleanest way to retrieve a key associated
with the maximum value of a hash. For instance, how do I retrieve 'b'
from the following example:

hash = Hash.new
hash['a']=4
hash['b']=10
hash['c']=7
How about Enumerable#max

hash = {'a'=>4,'b'=>10,'c'=>7}
max_record = hash.max { |a,b| a.last <=> b.last } # => ["b", 10]
max_index = max_record && max_record.first # => "b"
 
R

Robert Klemme

Simple question: what's the cleanest way to retrieve a key associated
with the maximum value of a hash. For instance, how do I retrieve 'b'
from the following example:

hash = Hash.new
hash['a']=4
hash['b']=10
hash['c']=7
How about Enumerable#max

hash = {'a'=>4,'b'=>10,'c'=>7}
max_record = hash.max { |a,b| a.last<=> b.last } # => ["b", 10]
max_index = max_record&& max_record.first # => "b"

In 1.9.x you can even spare the #last:

irb(main):001:0> hash = {'a'=>4,'b'=>10,'c'=>7}
=> {"a"=>4, "b"=>10, "c"=>7}
irb(main):002:0> hash.max {|(k1,v1),(k2,v2)| v1 <=> v2}
=> ["b", 10]

Here's a version with #inject

irb(main):004:0> hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ?
[k,v] : [maxk,maxv]}.last
=> 10

Kind regards

robert
 
H

Harry Kakueki

Simple question: =A0what's the cleanest way to retrieve a key associated
with the maximum value of a hash. =A0For instance, how do I retrieve 'b'
from the following example:

hash =3D Hash.new
hash['a']=3D4
hash['b']=3D10
hash['c']=3D7
--

What if you have more than one max?

h =3D {'a'=3D>4,'b'=3D>10,'c'=3D>7,'d' =3D> 10}

p h.select{|x,i| i=3D=3Dh.values.max}.keys #> ["b", "d"]


Harry
 
R

Rick DeNatale

Here's a version with #inject

irb(main):004:0> hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v]
: [maxk,maxv]}.last
=> 10

But that fails if there are only negative values in the hash.

hash = {:a => -2, :b => -4}
hash.inject([nil,0]) {|(maxk,maxv),(k,v)| v > maxv ? [k,v] :
[maxk,maxv]}.last # => 0

--
Rick DeNatale

Blog: http://talklikeaduck.denhaven2.com/
Github: http://github.com/rubyredrick
Twitter: @RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
 
A

Adam Prescott

This won't capture multiple maximum values, but:

hash.invert[hash.values.max]

I'd be interested to see a benchmark profile of the suggestions.
 
T

Timothy Baron

I've tried several of these approaches, and they all work well. I'm
curious, too, about which is the fastest, but for my small hashs,
there's no noticable difference.

Adam said:
This won't capture multiple maximum values, but:

hash.invert[hash.values.max]

I'd be interested to see a benchmark profile of the suggestions.
 
B

Ben Giddings

I've tried several of these approaches, and they all work well. I'm
curious, too, about which is the fastest, but for my small hashs,
there's no noticable difference.

Don't bother optimizing it for speed unless you are sure it's a
bottleneck. Choose whichever option is the most readable and
maintainable.

Ben
 
D

Douglas Seifert

[Note: parts of this message were removed to make it a legal post.]

For fun:

http://www.pastie.org/1153968

$ ruby -v
ruby 1.9.1p378 (2010-01-10 revision 26273) [x86_64-linux]
$ ruby max_hash_value.rb
{0=>-18, 1=>-5, 2=>-31, 3=>44, 4=>-13, 5=>-1, 6=>23, 7=>5, 8=>22, 9=>37}
user system total real
max 0.110000 0.000000 0.110000 ( 0.270811)
max 1.9.X 0.090000 0.000000 0.090000 ( 0.242181)
select 0.430000 0.000000 0.430000 ( 1.270329)
select (cache max) 0.080000 0.000000 0.080000 ( 0.147179)
sort_by 0.160000 0.000000 0.160000 ( 0.438671)
invert 0.080000 0.000000 0.080000 ( 0.279259)
invert (cache max) 0.060000 0.000000 0.060000 ( 0.155440)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top