0

I want to get all the keys with the max value.

# I want 6 and 2 since they both of them occurred three times.
arr = [5, 6, 2, 1, 2, 5, 6, 6, 2]

I tried this but it returns 6 only.

# this returns {5=>2, 6=>3, 2=>3, 1=>1}
freq = hash.inject(Hash.new(0)){ |h, v| h[v] += 1; h }
# then I use max_by, but this gives only 6
p arr.max_by{ |v| freq[v]}
2
  • Your question is a bit misleading: 'I want to get all the keys with the max value.' - max value in your array is 6 but then you ask for 2 as well... Do you want the most frequently repeated values? Commented May 20, 2014 at 12:44
  • Thanks for pointing out. I was thinking about freq hash. Commented May 20, 2014 at 12:50

4 Answers 4

4

I would propose a group_by on the number of occurrences of unique values in the original Array, followed by taking the values of the mapping with the highest number:

arr = [5, 6, 2, 1, 2, 5, 6, 6, 2]
arr.uniq.group_by { |v| arr.count(v) }.max.last
# [6, 2]

The group_by yields a Hash with the number of occurrences as keys and unique elements of arr with that number of occurrences as values. The .max call will then yield the mapping with the highest key, and .last will take only the values which we were interested in.

Sign up to request clarification or add additional context in comments.

7 Comments

Argh! I felt I was missing something :) I would use [1] instead of .last, is more coherent with I want the second value of an array converted from an hash, instead of I want the last element of an array
I get NoMethodError: undefined method `group_by' for [5, 6, 2, 1]:Array. rubyfiddle.com/riddles/926ab
@shin It's in Enumerable, at least for Ruby 2.0+, which version are you using? edit; and also for 1.9
Ok, let me check... Yep it works with 2.1.1. So rubyfiddle must be 1.8 then.
@shin I cannot find information about which version of Ruby is used by rubyfiddle.com, but clearly it's not up to date or just bugged. Even 1.8.7 had it ;(
|
3

What about

arr = [5, 6, 2, 1, 2, 5, 6, 6, 2]

uniques = arr.uniq
max_count = uniques.map { |v| arr.count(v) }.max
max_values = uniques.select { |v| arr.count(v) == max_count }
#=> [6, 2]

Comments

0

I think as

arr.group_by(&:to_i).each_with_object({}) do |(k,v),h| 
   (h[v.size] ||= []) << k 
end.max_by(&:first).last # => [6, 2] 

Comments

0
arr = [5, 6, 2, 1, 2, 5, 6, 6, 2]
freq = arr.inject(Hash.new(0)) { |h, v| h[v] += 1; h }   

max = freq.max[1]
freq.keep_if { |h, v| v == max }.keys
# => [6, 2]

The code is pretty self explanatory. I'm just using your code up until freq.keep_if. freq.max returns an array in the form [6, 3] (6 being the number, 3 being the occurrences), so freq.max[1] is the number of occurrences. .keys returns all the keys in an array, which gets rid of the occurrences.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.