[1, 1, 2].tally
# => { 1 => 2, 2 => 1 }[1, 1, 2].map(&:even?).tally
# => { false => 2, true => 1 }
[1, 2, 2, 3].tally
# => { 1 => 1, 2 => 2, 3 => 1 }
%w(foo foo bar foo baz foo).tally
=> {"foo"=>4, "bar"=>1, "baz"=>1}
%w(foo foo bar foo baz foo).tally_by { |s| s[0] }
=> {“f” => 4, “b” => 2}
Comments
Post a Comment