Ruby 如何计算数组中相同的元素.

mehdi-farsi · September 07, 2020 · 994 hits
a = %w[one two two three three three]

# Ruby 2.4+
a.group_by(&:itself).transform_values(&:count) # => {"one" => 1, "two" => 2, "three" => 3}

# Ruby 2.7+
a.tally # => {"one" => 1, "two" => 2, "three" => 3}

No Reply at the moment.
You need to Sign in before reply, if you don't have an account, please Sign up first.