Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > counting the number of repititions in an array

Reply
Thread Tools

counting the number of repititions in an array

 
 
Adam Akhtar
Guest
Posts: n/a
 
      01-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/.

 
Reply With Quote
 
 
 
 
Stefano Crocco
Guest
Posts: n/a
 
      01-30-2008
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[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}


> 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[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

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




 
Reply With Quote
 
 
 
 
Rie!
Guest
Posts: n/a
 
      01-30-2008
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,
li: http://linkedin.com/in/ariekeren,
fb: http://profile.to/ariekeren,
blog: http://tinyurl.com/2bjgvn }

 
Reply With Quote
 
Adam Akhtar
Guest
Posts: n/a
 
      01-30-2008
Wow thats excellent...just what i was looking for.

Cheers.

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

 
Reply With Quote
 
Adam Akhtar
Guest
Posts: n/a
 
      01-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[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.

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

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      01-30-2008
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[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.


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


 
Reply With Quote
 
Adam Akhtar
Guest
Posts: n/a
 
      01-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[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!
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Dober
Guest
Posts: n/a
 
      01-30-2008
A small 1.9 solution

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

Robert

 
Reply With Quote
 
Stefano Crocco
Guest
Posts: n/a
 
      01-31-2008
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


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-31-2008
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
counting number of uniques in a multidimensional array column Jack Perl Misc 22 08-10-2006 06:25 PM
Counting number of zeros in an array Nish Java 3 05-18-2006 07:22 PM
Counting number of zeros in an array Nishant C++ 4 05-18-2006 05:52 PM
Counting number of strings literals in double dimentional char array ranjmis C Programming 14 03-12-2006 11:51 PM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57