# counting the number of repititions in an array

Discussion in 'Ruby' started by Adam Akhtar, Jan 30, 2008.

Hi if i want to count the number of times values are repeated in an
array how do i go about it...is there a method?

My solution is

list.sort
loop through list and compare one element to the next and count
repetitions.

is there a simpler way?

Also

in C i used the for loop a lot to loop through arrays but i notice that
in ruby using
list.each do |element|
..
..
end

is quite popular.

If i wanted to do someting like this:

if list[currElement] == list[currElement + 1]
...

How would i go about writing that using list.each do |element|?

Any help greatly appreciated.
--
Posted via http://www.ruby-forum.com/.

2. ### Stefano CroccoGuest

Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
> Hi if i want to count the number of times values are repeated in an
> array how do i go about it...is there a method?
>
> My solution is
>
> list.sort
> loop through list and compare one element to the next and count
> repetitions.
>
> is there a simpler way?

a = [2,4,6,2,1,3,4,6,4,1,2,3,1]
res = Hash.new(0)
a.each do |i|
res+=1
end
p res

This creates a hash, which has a default value of 0 (that is, if a key isn't
included in the hash, the [] method returns 0). Then, there's an iteration on
all items of the array. For each element, the value of the hash item
corresponding to the array element is increased by one.

This can be written much more concisely as:

p a.inject(Hash.new(0)){|res, i| res+=1; res}

> in C i used the for loop a lot to loop through arrays but i notice that
> in ruby using
> list.each do |element|
> ..
> ..
> end
>
> is quite popular.
>
> If i wanted to do someting like this:
>
> if list[currElement] == list[currElement + 1]

Array#each_index or Array#each_with_index:

list.each_index do |i|
if list == list[i+1]
...
endif
end

or

list.each_with_index do |element, i|
if e == list[i+1]
...
endif
end

Of course, in both cases you'd need to handle yourself the case of the last
element (in which case, list[i+1] will return nil).

Another possibility, if you only need to access the item after the current one
is to use each_cons:

require 'enumerator'
a = [1,2,3,4]
a.each_cons(2){|i| p i}
=>
[1, 2]
[2, 3]
[3, 4]

I hope this helps

Stefano

> ...
>
> How would i go about writing that using list.each do |element|?
>
> Any help greatly appreciated.

Stefano Crocco, Jan 30, 2008

3. ### Rie!Guest

On 31/01/2008, Adam Akhtar <> wrote:

> Hi if i want to count the number of times values are repeated in an
> array how do i go about it...is there a method?
>
> My solution is
>
> list.sort
> loop through list and compare one element to the next and count
> repetitions.
>
> is there a simpler way?

do you mean this?

>> [3,9,7,1].inject { |i,j| i + j }

=> 20

> Also
>
> in C i used the for loop a lot to loop through arrays but i notice that
> in ruby using
> list.each do |element|
> ..
> ..
> end
>
> is quite popular.
>
> If i wanted to do someting like this:
>
> if list[currElement] == list[currElement + 1]
> ...
>
> How would i go about writing that using list.each do |element|?
>
> Any help greatly appreciated.
> --
> Posted via http://www.ruby-forum.com/.
>
>

--
r9 = { name: Rie!, ym: riyari3, skype: rubyninja,
fb: http://profile.to/ariekeren,
blog: http://tinyurl.com/2bjgvn }

Rie!, Jan 30, 2008

Stefano Crocco wrote:

> a = [2,4,6,2,1,3,4,6,4,1,2,3,1]
> res = Hash.new(0)
> a.each do |i|
> res+=1
> end
> p res
>
> This creates a hash, which has a default value of 0 (that is, if a key
> isn't
> included in the hash, the [] method returns 0). Then, there's an
> iteration on
> all items of the array. For each element, the value of the hash item
> corresponding to the array element is increased by one.
>

If the hash is empty to begin with, when you try to look up the key
using res
wont it just return 0. I cant see where the hash res is assigned with
the unique values from a.

Also is it possible to add more keys to a hash - i checked the instance
method section in the pick axe but there was nothign like pop or push.

--
Posted via http://www.ruby-forum.com/.

6. ### Stefano CroccoGuest

Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
> Stefano Crocco wrote:
> > a = [2,4,6,2,1,3,4,6,4,1,2,3,1]
> > res = Hash.new(0)
> > a.each do |i|
> > res+=1
> > end
> > p res
> >
> > This creates a hash, which has a default value of 0 (that is, if a key
> > isn't
> > included in the hash, the [] method returns 0). Then, there's an
> > iteration on
> > all items of the array. For each element, the value of the hash item
> > corresponding to the array element is increased by one.

>
> If the hash is empty to begin with, when you try to look up the key
> using res
> wont it just return 0. I cant see where the hash res is assigned with
> the unique values from a.

writing

var += something

is the same as writing

var = var + something

In fact, ruby actually translate the first form into the second. So, when I
write

res += 1

I mean:

res = res + 1

When the hash is emtpy (or it doesn't contain i), the call to res on the
right hand gives 0, so that res + 1 becomes 1. Then you have the
assignment:

res = 1

> Also is it possible to add more keys to a hash - i checked the instance
> method section in the pick axe but there was nothign like pop or push.

To add a new key to a hash, you should use the []= method:

hash = Hash.new #this creates an empty hash
hash['a'] = 1 # sets the value corresponding to the key 'a' to 1

You need to be careful: if the hash already contained the key 'a', the
previous value will be replaced by the new:

hash['b'] = 2
hash['b'] = 19

puts hash['b']
=> 19

Stefano

Stefano Crocco, Jan 30, 2008

Stefano Crocco wrote:
> Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
>> > included in the hash, the [] method returns 0). Then, there's an
>> > iteration on
>> > all items of the array. For each element, the value of the hash item
>> > corresponding to the array element is increased by one.

>>
>> If the hash is empty to begin with, when you try to look up the key
>> using res
>> wont it just return 0. I cant see where the hash res is assigned with
>> the unique values from a.

>
> writing
>
> var += something
>
> is the same as writing
>
> var = var + something
>
> In fact, ruby actually translate the first form into the second. So,
> when I
> write
>
> res += 1
>
> I mean:
>
> res = res + 1
>
> When the hash is emtpy (or it doesn't contain i), the call to res on
> the
> right hand gives 0, so that res + 1 becomes 1. Then you have the
> assignment:
>
> res = 1
>

thanks for the info on adding to hashes. I think i understand the res
= res + 1 bit. If we have an array such as [a, b, b, c, c, c] and
turn it into a hash like this
a -> 1
b -> 2
c -> 3

using your code above when i iterate through the array i do something
like this
res[a] = res[nil] + 1
res[a] = 0 + 1
res[a] = 1 #( a -> 1 ) right???
next iteration
res = res + 1
res = 0 + 1 #( b -> 1 ) right??
next iteration
res = res + 1
res = 1 + 1
res = 2 #ta da!!!!
and so on

is that how its working?

Got to say thats pretty slick!
--
Posted via http://www.ruby-forum.com/.

8. ### Robert DoberGuest

A small 1.9 solution

p 100.times.
map{rand(5)}.
inject(Hash.new(0)){|h,e| h.update e => h[e].succ}

Robert

Robert Dober, Jan 30, 2008
9. ### Stefano CroccoGuest

Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
> res[a] = res[nil] + 1

res[a] = res[a] + 1

Aside from this, yes, that's how it works

Stefano

Stefano Crocco, Jan 31, 2008
10. ### Robert KlemmeGuest

2008/1/31, Stefano Crocco <>:
> Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
> > res[a] = res[nil] + 1

>
> res[a] = res[a] + 1
>
> Aside from this, yes, that's how it works

Just adding a point because it seems this might not have become
totally clear from the thread: the fact that the Hash returns 0 for a
missing key is caused by how the Hash is constructed. Normally a hash
returns nil for unknown keys:

irb(main):001:0> {}[1]
=> nil
irb(main):002:0> Hash.new(0)[1]
=> 0
irb(main):003:0> Hash.new("foobar")[1]
=> "foobar"

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

Robert Klemme, Jan 31, 2008