3

I can imagine there is a simple way to do that instead of use many variables and state.

I just want to get the highest value given for each key in a list of hashes

For example:

[{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]

Result

[{1=>19.4}, {3=>12.4}, {2=>59.4}]
0

5 Answers 5

5

I'd do as below :

a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]

# the below is the main trick, to group the hashes and sorted the key/value pair
# in **ascending order**.
a.sort_by(&:to_a)
# => [{1=>12.4}, {1=>19.4}, {2=>29.4}, {2=>39.4}, {2=>59.4}, {3=>12.4}]

# then using the trick in mind, we know hash can't have duplicate keys, so
# blindly I can rely on `Enumerable#inject` with the `Hash#merge` method.
a.sort_by(&:to_a).inject(:merge)
# => {1=>19.4, 2=>59.4, 3=>12.4}

# final one
a.sort_by(&:to_a).inject(:merge).map { |k,v| {k => v} }
# => [{1=>19.4}, {2=>59.4}, {3=>12.4}]
1
  • @coffee Its for yours.. :) Feb 12, 2014 at 13:33
3

This is a variant of @Matt's answer:

 a.group_by(&:keys).map {|k,v| {k.first => v.map(&:values).flatten.max}}
   #=> [{1=>19.4}, {2=>59.4}, {3=>12.4}]
3

How about this?

Hash[a.flat_map(&:to_a).sort_by(&:last)]
# a.flat_map(&:to_a).sort_by(&:last).to_h for Ruby 2.1+ as @steenslag suggested
=> {3=>12.4, 1=>19.4, 2=>59.4}

Here's the fruity benchmark comparison:

require 'fruity'
a = 1000.times.collect { |i| { rand(100) => rand(1000) } }

compare do
  caryswoveland { 
   a.group_by(&:keys).map {|k,v| {k.first => v.map(&:values).flatten.max}} 
  }
  matt { a.group_by(&:keys).map { |k,v| v.max_by { |j| j[k[0]] } } }
  steenslag { 
    a.each_with_object({}){|h, res| 
      res.merge!(h){|k, *vals| res[k] = vals.max} 
    } 
  }
  abdo { Hash[a.flat_map(&:to_a).sort_by(&:last)] }
end

Output:

Running each test 4 times. Test will take about 1 second.
abdo is similar to steenslag (results differ..)
steenslag is faster than matt by 30.000000000000004% ± 10.0%
matt is faster than caryswoveland by 30.000000000000004% ± 10.0%
8
  • Nice and compact, Abdo, but a couple of problems: your output is a hash, whereas an array of hashes is required and sort is more time-consuming than max. You should be able to fix the first problem easily. I initially made the same mistake, and believe a hash may be better here. Feb 11, 2014 at 22:47
  • Agreed that it's not the required result.. I think I like my hash better though haha :-) You're right about sort but it looks like according to the benchmark, it's being faster.. weird
    – Abdo
    Feb 11, 2014 at 22:59
  • p a.flat_map(&:to_a).sort_by(&:last).to_h (Ruby 1.2.1) is really starting to look good
    – steenslag
    Feb 11, 2014 at 23:09
  • Interesting test results. Could you please change 100 and 1000 to something larger and report the effect that has? I'd be interested in knowing if sort will maintain its dominance. Feb 11, 2014 at 23:24
  • I did a = 10000.times.collect { |i| { rand(1000) => rand(10000) } }, steenslag is 10% +- 10% faster than abdo, abdo is 20% +- 10% faster than matt and caryswoveland. I then did a = 10000.times.collect { |i| { rand(10000) => rand(10000) } } (more dispersed keys), steenslag is ~70% faster than abdo, abdo ~50% faster than matt and matt is ~80% faster than caryswoveland.
    – Abdo
    Feb 11, 2014 at 23:56
1
a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]

p a.each_with_object({}){|h, res| res.merge!(h){|k, *vals| res[k] = vals.max} }
#=> {1=>19.4, 2=>59.4, 3=>12.4}
2
  • steen, consider each_with_object({}). Feb 11, 2014 at 22:00
  • @CarySwoveland Yeah I know it's better style. Hate the name though. Why not each_with ?
    – steenslag
    Feb 11, 2014 at 22:13
1
a = [{1=>19.4}, {1=>12.4}, {2=>29.4}, {3=>12.4}, {2=>39.4}, {2=>59.4}]

a.group_by(&:keys).map { |k,v| v.max_by { |j| j[k[0]] } }

[{1=>19.4}, {2=>59.4}, {3=>12.4}]

Credit to Cary Swoveland for group_by(&:keys).

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.