Full disclosure: I authored and maintain the hash
package.
Unless you have a hash with many key-value pairs and need the performance, standard R vectors with names will likely be a better solution. Here is one example:
v <- c(a = 5, b = 2, c = 3, d = 5)
names( v[ v==max(v) ] )
Native R vectors will outperform hashes until the structure grows beyond ~200 key-value pairs. (It is been a while since I benchmarked hash, vector and list lookup performance).
If a hash fits the solution, the answer by @bergant solves the OP's questions, though please understand it is rather dangerous. Converting a hash to a list and then using unlist
ignores the fact that hash values are not constrained to be scalar/atomic values. They can be any R object. Consider:
> hash(a = 1:5, b = 2, c = 3, d=5)
<hash> containing 4 key-value pair(s).
a : 1 2 3 4 5
b : 2
c : 3
d : 5
You can decide whether this is a problem for your application or not.
A simpler, higher performing and more general approach is to use the 'values' function. In the simple case where all values are scalar/atomic values, this closely mirrors @bergant's solution.
H <- hash(a = 5, b = 2, c = 3, d = 5)
val <- values(H) # Compare to `unlist(as.list(H))`
names( val[ val == max(val) ] )
Since values returns a named list rather than an unlisted, we are set up for the more general solution since we can select a value to compare from each key value pair:
H <- hash(a = 1:5, b = 2, c = 3, d=5)
val <- values(H)
# Alternate 1: Compare min from each value
val <- sapply(val, max )
# Alternate 2: Compare first element from each value
# val <- sapply(val, function(x) x[[1]])
names( val[ val == max(val) ] )
I hope that helps.