Count similar items in an array in Ruby
Before ruby 2.7, you would have to do some hacky code like:
array.inject({}) {|f, v| f[v] ||= 0; f[v] += 1 ; f }
But starting with Ruby 2.7, there's a nicer way:
array.tally
irb: [1, 2, 3, 4, 4, 5, 5].tally
=> {1=>1, 2=>1, 3=>1, 4=>2, 5=>2}