Answers for "how to find the frequency of a number in an array ruby"

0

ruby find frequency in array self.all

balls = [m1, m2, m3, m4]
most_idx = nil

groups = balls.inject({}) do |hsh, ball|
  hsh[ball.color] = [] if hsh[ball.color].nil?
  hsh[ball.color] << ball

  most_idx = ball.color if hsh[most_idx].nil? || hsh[ball.color].size > hsh[most_idx].size 
  hsh
end

groups[most_idx] # => [m1,m2,m4]
Posted by: Guest on July-19-2020

Code answers related to "how to find the frequency of a number in an array ruby"

Browse Popular Code Answers by Language