Created
August 1, 2018 15:43
-
-
Save djuber/4556c25a4695014958d436fc384f0e0f to your computer and use it in GitHub Desktop.
array count items
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Array | |
# group elements by their identity, and count each distinct item | |
def counts | |
group_by(&:itself).map {|k,v| [k, v.count] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this becomes Enumerable#tally in ruby 2.7 and it is implemented more efficiently.