# Re: Making a count of unique elements in an array

**URL:** <https://www.ruby-forum.com/t/re-making-a-count-of-unique-elements-in-an-array/51917>\
**Category:** Ruby\
**Created:** [January 9, 2006, 8:09am UTC](https://www.ruby-forum.com/t/re-making-a-count-of-unique-elements-in-an-array/51917 "2006-01-09T08:09:25Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![PeSSSSaS\_Botp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ecd19e/32.png) [@PeSSSSaS\_Botp](https://www.ruby-forum.com/u/PeSSSSaS_Botp)\
**Post date:** [January 9, 2006, 8:09am UTC](https://www.ruby-forum.com/t/re-making-a-count-of-unique-elements-in-an-array/51917/1 "2006-01-09T08:09:25Z")

</div>

Dan K. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] wrote:

#counter = {}  
#arr.each {|e| counter[e] += 1 rescue counter[e] = 1 }  
#p counter.to\_a

me, like the above but w slight modif below:

irb(main):013:0\> counter2 = Hash.new(0)  
=\> {}  
irb(main):014:0\> arr.each {|e| counter2[e] += 1 }  
=\> [“a”, “b”, “b”, “c”, “c”, “d”, “e”, “e”, “e”]  
irb(main):015:0\> counter2.to\_a  
=\> [[“a”, 1], [“b”, 2], [“c”, 2], [“d”, 1], [“e”, 3]]  
irb(main):016:0\>

(note the plain counter and the hash param)

hth.

kind regards -botp
