Another version of a hash with a key for each element in your array and value for the count of each element
a = [ 1, 2, 3, 3, 4, 3]
h = Hash.new(0)
a.each { | v | h.store(v, h[v]+1) }
# h = { 3=>3, 2=>1, 1=>1, 4=>1 }
More Related Contents:
- How to count identical string elements in a Ruby array
- how to i add objects to an array ? ruby
- How do I pick randomly from an array?
- How to sum array of numbers in Ruby?
- How to check if a value exists in an array in Ruby
- How to select unique elements
- Ruby array access 2 consecutive(chained) elements at a time
- How to find and return a duplicate value in array
- Array#each vs. Array#map
- What does %w(array) mean?
- How to split (chunk) a Ruby array into parts of X elements? [duplicate]
- How to count duplicate elements in a Ruby array
- Find indices of elements that match a given condition
- Creating matrix with `Array.new(n, Array.new)`
- Difference between map and collect in Ruby?
- Create two-dimensional arrays and access sub-arrays in Ruby
- How do I search within an array of hashes by hash values in ruby?
- What is the best way to convert an array to a hash in Ruby
- Performance of Arrays and Hashes in Ruby
- Find key/value pairs deep inside a hash containing an arbitrary number of nested hashes and arrays
- Ruby method Array#
- In Ruby, why does Array.new(size, object) create an array consisting of multiple references to the same object?
- How to chunk an array in Ruby
- Ruby – Difference between Array#
- Sort an array according to the elements of another array
- How do you add an array to another array in Ruby and not end up with a multi-dimensional result?
- Ruby: filter array by regex?
- Ruby array subtraction without removing items more than once
- Where is it legal to use ruby splat operator?
- How do I remove blank elements from an array?