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


Groups > comp.lang.ruby > #6906

Re: Making a count of unique elements in an array

X-Received by 10.58.249.208 with SMTP id yw16mr17046853vec.10.1388094263689; Thu, 26 Dec 2013 13:44:23 -0800 (PST)
X-Received by 10.49.74.134 with SMTP id t6mr55858qev.14.1388094263627; Thu, 26 Dec 2013 13:44:23 -0800 (PST)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!usenet.blueworldhosting.com!feeder01.blueworldhosting.com!peer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!p15no44310385qaj.0!news-out.google.com!l9ni207qay.0!nntp.google.com!p15no44310374qaj.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.ruby
Date Thu, 26 Dec 2013 13:44:23 -0800 (PST)
In-Reply-To <bh3etcFg4fbU1@mid.individual.net>
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=75.97.112.47; posting-account=TdiImQoAAABMHsswBDfZ_U7gDZmJueFb
NNTP-Posting-Host 75.97.112.47
References <1136789236.482098.144200@o13g2000cwo.googlegroups.com> <5327790f-e3da-4cb6-a94c-b198de7139da@googlegroups.com> <bh3etcFg4fbU1@mid.individual.net>
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <9d904cf9-f46c-4ec9-8564-45614ff97501@googlegroups.com> (permalink)
Subject Re: Making a count of unique elements in an array
From lukeabergen@gmail.com
Injection-Date Thu, 26 Dec 2013 21:44:23 +0000
Content-Type text/plain; charset=ISO-8859-1
X-Received-Bytes 1441
X-Received-Body-CRC 343750300
Xref csiph.com comp.lang.ruby:6906

Show key headers only | View raw


Another way is the super-useful group_by and map

arr = ["a", "b", "b", "c", "c", "d", "e", "e", "e"]
grouped = arr.group_by {|x| x}  #=> produces something like {"a" => ["a"], "b" => ["b", "b"], etc...
result = grouped.map {|el, arr| [el, arr.count]}

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


Thread

Re: Making a count of unique elements in an array vsingla160@gmail.com - 2013-12-12 09:56 -0800
  Re: Making a count of unique elements in an array Norbert Melzer <norbert.melzer@gmx.net> - 2013-12-12 19:44 +0100
  Re: Making a count of unique elements in an array Robert Klemme <shortcutter@googlemail.com> - 2013-12-14 17:22 +0100
    Re: Making a count of unique elements in an array lukeabergen@gmail.com - 2013-12-26 13:44 -0800
      Re: Making a count of unique elements in an array Robert Klemme <shortcutter@googlemail.com> - 2013-12-28 12:02 +0100

csiph-web