I have a hash (in Perl) where the values are all numbers. I need to create another hash that contains all key/value pairs from the first hash where the value is the maximum of all values.
For example, given
my %hash = (
key1 => 2,
key2 => 6,
key3 => 6,
);
I would like to create a new hash containing:
%hash_max = (
key2 => 6,
key3 => 6,
);
I’m sure there are many ways to do this, but am looking for an elegant solution (and an opportunity to learn!).
Or a one-pass approach (similar to but slightly different from another answer):