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}]
I'd do as below :
How about this?
Here's the
fruity
benchmark comparison:Output:
Credit to Cary Swoveland for
group_by(&:keys)
.This is a variant of @Matt's answer: