# Counting the number of repititions in an array

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.

Alle Wednesday 30 January 2008, Adam A. 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[i]+=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[i]+=1; res}

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

Array#each_index or Array#each_with_index:

list.each_index do |i|
if list[i] == 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

On 31/01/2008, Adam A. [email protected] 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

Stefano C. wrote:

a = [2,4,6,2,1,3,4,6,4,1,2,3,1]
res = Hash.new(0)
a.each do |i|
res[i]+=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[i]
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.

Wow thats excellent…just what i was looking for.

Cheers.

Stefano C. wrote:

Alle Wednesday 30 January 2008, Adam A. 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[i]
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[i] += 1

I mean:

res[i] = res[i] + 1

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

res[i] = 1

thanks for the info on adding to hashes. I think i understand the res[i]
= res[i] + 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[b] = res[b] + 1
res[b] = 0 + 1 #( b -> 1 ) right??
next iteration
res[b] = res[b] + 1
res[b] = 1 + 1
res[b] = 2 #ta da!!!
and so on

is that how its working?

Got to say thats pretty slick!

A small 1.9 solution

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

Robert

Alle Wednesday 30 January 2008, Adam A. ha scritto:

res[a] = res[nil] + 1

res[a] = res[a] + 1

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

Stefano

Alle Wednesday 30 January 2008, Adam A. 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[i]
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[i] += 1

I mean:

res[i] = res[i] + 1

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

res[i] = 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

2008/1/31, Stefano C. [email protected]:

Alle Wednesday 30 January 2008, Adam A. 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