From: Rie! Date: 2008-01-31T05:38:48+09:00 Subject: Re: counting the number of repititions in an array 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 }