Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #4563

Re: Counting how many times the same elements occurs in an array?

From Robert Klemme <shortcutter@googlemail.com>
Newsgroups comp.lang.ruby
Subject Re: Counting how many times the same elements occurs in an array?
Date 2011-05-15 13:52 +0200
Message-ID <939t4dF4vaU1@mid.individual.net> (permalink)
References <6a81112b3af069fa30e1c843c72f8be9@ruby-forum.com>

Show all headers | View raw


On 15.05.2011 12:01, Thomas Greenwood wrote:
> There's probably a fairly simple way to do this.
>
> Basically I'm reading data from an xml file, I need to figure out how
> many times identical data occurs in certain attributes, so far I've got
> the data into two identical arrays and had the intention of nesting
> iterators - seeing if the element was equal to the second and
> incrementing every time a match was found. That obviously didn't work
> out the way I initially thought.
>
> This seems to be the jist of what I want but it's obviously returning a
> count on every iteraton whereas I only want the final tally.
>
> xml_events.each{|x|
>    puts "#{x} occurs #{xml_events.count(x)} times"
> }
>
> Any ideas?

Two possible approaches:

irb(main):002:0> a = Array.new(10) { rand(4) }
=> [3, 2, 2, 1, 3, 3, 2, 3, 3, 3]

irb(main):003:0> a.inject(Hash.new(0)) {|sums,x| sums[x] += 1; sums}
=> {3=>6, 2=>3, 1=>1}

irb(main):004:0> a.group_by {|x| x}
=> {3=>[3, 3, 3, 3, 3, 3], 2=>[2, 2, 2], 1=>[1]}
irb(main):005:0> a.group_by {|x| x}.map {|k,v| [k, v.size]}
=> [[3, 6], [2, 3], [1, 1]]

Instead of #inject you can of course also use a more traditional approach:

irb(main):012:0> counts = Hash.new 0
=> {}
irb(main):013:0> a.each {|x| counts[x] += 1}
=> [3, 2, 2, 1, 3, 3, 2, 3, 3, 3]
irb(main):014:0> counts
=> {3=>6, 2=>3, 1=>1}

Kind regards

	robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Counting how many times the same elements occurs in an array? Thomas Greenwood <nedpointsman@gmail.com> - 2011-05-15 05:01 -0500
  Re: Counting how many times the same elements occurs in an array? John Feminella <johnf@bitsbuilder.com> - 2011-05-15 06:49 -0500
  Re: Counting how many times the same elements occurs in an array? Robert Klemme <shortcutter@googlemail.com> - 2011-05-15 13:52 +0200
  Re: Counting how many times the same elements occurs in an array? Thomas Greenwood <nedpointsman@gmail.com> - 2011-05-15 08:06 -0500
    Re: Counting how many times the same elements occurs in an array? Robert Klemme <shortcutter@googlemail.com> - 2011-05-15 15:37 +0200
    Re: Counting how many times the same elements occurs in an array? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-05-15 15:01 -0500
      Re: Counting how many times the same elements occurs in an array? Adam Prescott <adam@aprescott.com> - 2011-05-15 15:21 -0500
      Re: Counting how many times the same elements occurs in an array? David Jacobs <developer@wit.io> - 2011-05-15 15:33 -0500

csiph-web