counting the number of repititions in an array

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

  1. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Jan 30, 2008
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. Adam Akhtar

    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,
    li: http://linkedin.com/in/ariekeren,
    fb: http://profile.to/ariekeren,
    blog: http://tinyurl.com/2bjgvn }
     
    Rie!, Jan 30, 2008
    #3
  4. Adam Akhtar

    Adam Akhtar Guest

    Adam Akhtar, Jan 30, 2008
    #4
  5. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Jan 30, 2008
    #5
  6. 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
    #6
  7. Adam Akhtar

    Adam Akhtar Guest

    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/.
     
    Adam Akhtar, Jan 30, 2008
    #7
  8. Adam Akhtar

    Robert Dober Guest

    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
    #8
  9. 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
    #9
  10. 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
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nish
    Replies:
    3
    Views:
    819
    Patricia Shanahan
    May 18, 2006
  2. ranjmis
    Replies:
    14
    Views:
    522
    Ben C
    Mar 12, 2006
  3. Nishant
    Replies:
    4
    Views:
    1,569
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    May 18, 2006
  4. Jack
    Replies:
    22
    Views:
    263
    Ted Zlatanov
    Aug 10, 2006
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    206
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page