my data is in a file basically like :
a
2
b
6
a
4
f
2
b
1
a
7
I have this hash :
%hash = {
a => 2,
b => 6,
a => 4,
f => 2,
b => 1,
a => 7,
};
How can I find duplicate keys and among them? I want the one that have the biggest value.
Desired output:
a-->7
b-->6
f-->2
If you only ever want the highest value for a particular key, then add the logic into your assignment to the hash. At the point you would add a key and a value, do something like this:
If you need to preserve all values, then make each key point to an array of values. Here is what your assignment would look like:
Here is a nice way to find the maximum value in the resultant array for a given key: