# Ruby array count

**URL:** <https://www.ruby-forum.com/t/ruby-array-count/118583>\
**Category:** Rails\
**Created:** [October 29, 2007, 7:07am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583 "2007-10-29T07:07:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 7:07am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/1 "2007-10-29T07:07:20Z")

</div>

Hi Experts,

arr = [1, 2, 3, 5, 5, 2, 5]  
is that possible to find whats the count of an value inside an array  
“arr”

for example

1 - 1  
2 - 2  
3 - 1  
5 - 3

is that possible?

plz help me guys, its my requirement for specific reasons.

regards,  
Bala

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 8:37am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/2 "2007-10-29T08:37:53Z")

</div>

Hi,

On 29 Okt., 07:06, Bala [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> arr = [1, 2, 3, 5, 5, 2, 5]  
> is that possible to find whats the count of an value inside an array  
> “arr”

One way could be something like this.

arr = [1, 2, 3, 5, 5, 2, 5]  
counts = {}  
arr.each do |item|  
begin  
counts[item] = counts[item] + 1  
rescue  
counts[item] = 1  
end  
end  
counts.each {|k, v| puts “#{k}: #{v}”}

But there probably is some more concise way.

Lutz

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 8:40am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/3 "2007-10-29T08:40:13Z")

</div>

Hi Lutz,

I got it, but i tried in way using like this

class Array  
def count  
k=Hash.new(0)  
self.each{|x| k[x]+=1 }  
k  
end  
end

a = %w[N1100, N2200, N2200, N1100, N4400]  
items = a.count  
puts items.inspect

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 8:50am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/4 "2007-10-29T08:50:25Z")

</div>

Bala wrote:

> Hi Lutz,
> 
> I got it, but i tried in way using like this
> 
> class Array  
> def count  
> k=Hash.new(0)  
> self.each{|x| k[x]+=1 }  
> k  
> end  
> end
> 
> a = %w[N1100, N2200, N2200, N1100, N4400]  
> items = a.count  
> puts items.inspect

inject is QUITE a powerful tool as well  
[http://www.ruby-doc.org/core/classes/Enumerable.html#M003171](http://www.ruby-doc.org/core/classes/Enumerable.html#M003171)

arr = [1, 2, 3, 5, 5, 2, 5]  
arr.inject(Hash.new(0)){|hsh,arr\_elm| hsh[arr\_elm]+=1 }

worth checking out, a lot of functionality in a one-liner… worth  
knowing exists, at the least. hth -

shai

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 8:55am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/5 "2007-10-29T08:55:24Z")

</div>

Shai R. wrote:

> Shai R. wrote:
> 
> > Bala wrote:
> > 
> > > Hi Lutz,
> > > 
> > > I got it, but i tried in way using like this
> > > 
> > > class Array  
> > > def count  
> > > k=Hash.new(0)  
> > > self.each{|x| k[x]+=1 }  
> > > k  
> > > end  
> > > end
> 
> …btw, these types of questions (pure ruby vs ruby on rails) are great  
> to ask on the ruby forum - that’s what it is for, and the real experts  
> seem to lay around there (i’ve seen a DHH and even matz shmooze around  
> there occasionally)

…and, for example, a thread i’ve just found on  
the ruby-forum right at this moment -

[http://www.ruby-forum.com/topic/129613](http://www.ruby-forum.com/topic/129613)

- guess what it’s about  
;p

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 9:04am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/6 "2007-10-29T09:04:47Z")

</div>

Thanks a lot Experts!!! it works perfectly. 🙂

On Oct 29, 12:55 pm, Shai R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Bala](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/94ad74/32.png) [@Bala](https://www.ruby-forum.com/u/Bala)\
**Post date:** [October 29, 2007, 8:52am UTC](https://www.ruby-forum.com/t/ruby-array-count/118583/7 "2007-10-29T08:52:43Z")

</div>

Shai R. wrote:

> Bala wrote:
> 
> > Hi Lutz,
> > 
> > I got it, but i tried in way using like this
> > 
> > class Array  
> > def count  
> > k=Hash.new(0)  
> > self.each{|x| k[x]+=1 }  
> > k  
> > end  
> > end

…btw, these types of questions (pure ruby vs ruby on rails) are great  
to ask on the ruby forum - that’s what it is for, and the real experts  
seem to lay around there (i’ve seen a DHH and even matz shmooze around  
there occasionally)
