-1
a = [1, 2, 56, 3, 56, 7, 7, 7, 56, 40, 40, 40]
a.group_by do |e|                                            
    e                                                               
end.values.max_by(&:size).first

This return 56 but how we do to return all the common values like: [56,7,40]

We can create a hash with all repetitions and after with select get the numbers with the max repetitions value. But maybe there are a easier way to do this.

Héctor León
  • 2,210
  • 2
  • 23
  • 36

1 Answers1

1
a.select { |e| a.count(e) > 1 }.uniq
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160