📜  ruby 在数组 self.all 中查找频率 - Ruby 代码示例

📅  最后修改于: 2022-03-11 15:04:48.790000             🧑  作者: Mango

代码示例1
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]