Skip to content

Instantly share code, notes, and snippets.

@djuber
Created August 1, 2018 15:43
Show Gist options
  • Save djuber/4556c25a4695014958d436fc384f0e0f to your computer and use it in GitHub Desktop.
Save djuber/4556c25a4695014958d436fc384f0e0f to your computer and use it in GitHub Desktop.
array count items
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
@djuber
Copy link
Author

djuber commented Oct 19, 2020

this becomes Enumerable#tally in ruby 2.7 and it is implemented more efficiently.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment