counting the number of repititions in an array

A

Adam Akhtar

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.
 
S

Stefano Crocco

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
 
R

Rie!

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.
 
A

Adam Akhtar

Stefano said:
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.
 
S

Stefano Crocco

Alle Wednesday 30 January 2008, Adam Akhtar ha scritto:
Stefano said:
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
 
A

Adam Akhtar

Stefano said:
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!
 
R

Robert Dober

A small 1.9 solution

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

Robert
 
S

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

Stefano
 
R

Robert Klemme

2008/1/31 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top