# Can i calculate the data number in array?

**URL:** <https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003>\
**Category:** Ruby\
**Created:** [June 28, 2007, 10:30am UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003 "2007-06-28T10:30:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 10:30am UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/1 "2007-06-28T10:30:10Z")

</div>

```auto
Array = ["a", "a", "a", "b", "b", â€œcâ€, "c", "c","d", "d", "e"]
```

Can i calculate the data in the Array???

a a a b b c c c d d e

For example: ArrayNumber = [“3”, “2”, “3”, “2”, “1”]

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 10:42am UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/2 "2007-06-28T10:42:20Z")

</div>

d=[“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]  
p d

dd={}  
d.each do |v|  
dd[v]||=0  
dd[v]+=1  
end

p dd

p dd.values

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 10:43am UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/3 "2007-06-28T10:43:58Z")

</div>

gz zz wrote:

> d=[“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]  
> p d
> 
> dd={}  
> d.each do |v|  
> dd[v]||=0  
> dd[v]+=1  
> end
> 
> p dd
> 
> p dd.values

But,I think ruby’s Hash is not ordered,so…

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 10:45am UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/4 "2007-06-28T10:45:50Z")

</div>

Dear Cool,

maybe this is what you want:

class Array

def count

```
k=Hash.new(0)

self.each{ |x| k[x]+=1 }

k

```

end

end

my\_array = [“a”, “a”, “a”, “b”, “b”, â€œcâ€, “c”, “c”,“d”, “d”, “e”]  
my\_array.count

It gives you a Hash, though, and the numbers are Integers,  
not Strings, but this seems more reasonable 🙂

Best regards,

Axel

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 12:11pm UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/5 "2007-06-28T12:11:02Z")

</div>

On 28.06.2007 10:44, Axel E. wrote:

> ```
> k=Hash.new(0)
> 
> ```
> 
> my\_array = [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”,“d”, “d”, “e”]  
> my\_array.count
> 
> It gives you a Hash, though, and the numbers are Integers,  
> not Strings, but this seems more reasonable 🙂

Yes, I’d agree. Having tuples as return value is better because then  
you maintain relationship between items and item count.

Btw, thanks for leaving the inject solution to me. 🙂

irb(main):005:0\> a = %w{a a a b b c c c d d e}  
=\> [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “e”]  
irb(main):006:0\> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}  
=\> {“a”=\>3, “b”=\>2, “c”=\>3, “d”=\>2, “e”=\>1}  
irb(main):007:0\> a.inject(Hash.new(0)) {|h,x| h[x]+=1; h}.sort  
=\> [[“a”, 3], [“b”, 2], [“c”, 3], [“d”, 2], [“e”, 1]]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 12:57pm UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/6 "2007-06-28T12:57:09Z")

</div>

On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Btw, thanks for leaving the inject solution to me. 🙂  
> Shame on you!!! 😉  
> But that is not what OP asked for

(a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use  
#=== here

More generally we have therefore

(a|[]).map{|e|a.select{|x|x==e}.size}

Maybe OP preferred  
…).sort.map{…

and sorry OP, I just needed this working break.

Robert

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/7 "2007-06-28T13:52:14Z")

</div>

On 28.06.2007 12:56, Robert D. wrote:

> On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Btw, thanks for leaving the inject solution to me. 🙂  
> > Shame on you!!! 😉

Um, why? Somehow I don’t get the joke, sorry. 🙂

> But that is not what OP asked for
> 
> (a|[]).map{|e|a.grep(e).size} ### I know it might not work as we use  
> #=== here

Sorry, Robert, but I don’t think this is what the OP was asking for:

irb(main):001:0\> a = %w{a a a b b c c c d d e}  
=\> [“a”, “a”, “a”, “b”, “b”, “c”, “c”, “c”, “d”, “d”, “e”]  
irb(main):002:0\> a.map{|e|a.grep(e).size}  
=\> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]

Your solution repeats counts for the same string but as far as I can see  
from his original posting he wants counts for each string just once.

> More generally we have therefore
> 
> (a|[]).map{|e|a.select{|x|x==e}.size}
> 
> Maybe OP preferred  
> …).sort.map{…
> 
> and sorry OP, I just needed this working break.

🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/8 "2007-06-28T23:20:54Z")

</div>

On 28.06.2007 16:11, Robert D. wrote:

> On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > On 28.06.2007 12:56, Robert D. wrote:
> > 
> > > On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > =\> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]  
> > Careful here Robert I wrote
> 
> (a|[]) which is a more computation intensive way to write a.uniq. –  
> its a coding joke brrr

Umpf! I actually overlooked that one. Somehow I read it as (a||[]).  
Duh! Sorry for the noise. Btw, that idiom was not part of my  
repertoire. So I actually learned something today - although I have to  
say I’d rather stick with #uniq - it’s easier for my brain. 🙂

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![coolwong](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/d9b06d/32.png) [@coolwong](https://www.ruby-forum.com/u/coolwong)\
**Post date:** [June 28, 2007, 4:12pm UTC](https://www.ruby-forum.com/t/can-i-calculate-the-data-number-in-array/108003/9 "2007-06-28T16:12:39Z")

</div>

On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On 28.06.2007 12:56, Robert D. wrote:
> 
> > On 6/28/07, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> > 
> > > Btw, thanks for leaving the inject solution to me. 🙂  
> > > Shame on you!!! 😉
> 
> Um, why? Somehow I don’t get the joke, sorry. 🙂  
> Really or are you metajoking?anyway to get out of recursive joke mode,  
> we are sometimes competing about #inject.  
> irb(main):002:0\> a.map{|e|a.grep(e).size}  
> =\> [3, 3, 3, 2, 2, 3, 3, 3, 2, 2, 1]  
> Careful here Robert I wrote

(a|[]) which is a more computation intensive way to write a.uniq. –  
its a coding joke brrr

Read my post please you can learn sooooo much. – this is a joke!

Robert
