Skip to content

Instantly share code, notes, and snippets.

@covard
Last active March 1, 2021 21:49
Show Gist options
  • Save covard/fbabf356090819239fda496ffcce57cb to your computer and use it in GitHub Desktop.
Save covard/fbabf356090819239fda496ffcce57cb to your computer and use it in GitHub Desktop.
Ruby array group / count same value to hash

Array Group By Same Value

2.7.1 :001 > [1, 2, 3, 4, 5, 6, 6, 5, 7, 8, 9, 0].group_by(&:itself).map { |k, v| [k, v.count] }.to_h
=> {1=>1, 2=>1, 3=>1, 4=>1, 5=>2, 6=>2, 7=>1, 8=>1, 9=>1, 0=>1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment