Given an array, I need to produce a two-dimensional resulting array
where each pair consists of a unique element from the original array
and the number of times that element appears.
I found two ways to do this. Is one of them better? Is there a better
way?
arr = %w{a b b c c d e e e}
p arr
p arr.uniq.map {|e| [e, (arr.select {|ee| ee == e}).size ]}
counter = {}
arr.each {|e| counter[e] += 1 rescue counter[e] = 1 }
p counter.to_a
outputs:
["a", "b", "b", "c", "c", "d", "e", "e", "e"]
[["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]
[["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]