Get count of individual items in an array
array.inject(Hash.new(0)){|result, element| result[element] += 1; result }
["a", "b", "a", "c", "d", "a", "b"] => {"a"=>3, "b"=>2, "c"=>1, "d"=>1}
Written by Dipil Saud
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Related Tags
#ruby
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#