Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Norbert Melzer Newsgroups: comp.lang.ruby Subject: Re: Making a count of unique elements in an array Date: Thu, 12 Dec 2013 19:44:47 +0100 Lines: 29 Message-ID: References: <1136789236.482098.144200@o13g2000cwo.googlegroups.com> <5327790f-e3da-4cb6-a94c-b198de7139da@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252 Content-Transfer-Encoding: 8bit X-Trace: individual.net lKiqepd5DVjXmkklFGRl/AXeWAuWI0uovSnj78DE03Rak52pDT Cancel-Lock: sha1:UxMHCeV/XSjO4qe6plDmwlrlG9o= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.1.1 In-Reply-To: <5327790f-e3da-4cb6-a94c-b198de7139da@googlegroups.com> Xref: csiph.com comp.lang.ruby:6897 On 12.12.2013 18:56, vsingla160@gmail.com wrote: > if i have to count no. of distinct elements in a 2D array ,then what > to do?? I think at least one of this will do it: ``` [1] pry(main)> [[1,2],[2,3],[1,2]].count => 3 [2] pry(main)> [[1,2],[2,3],[1,2]].uniq => [[1, 2], [2, 3]] [3] pry(main)> [[1,2],[2,3],[1,2]].uniq.count => 2 [4] pry(main)> [[1,2],[2,3],[1,2]].flatten => [1, 2, 2, 3, 1, 2] [5] pry(main)> [[1,2],[2,3],[1,2]].flatten.uniq => [1, 2, 3] [6] pry(main)> [[1,2],[2,3],[1,2]].flatten.uniq.count => 3 ``` There is a very beautiful ressource: You can find nearly anything about arrays there… HTH Norbert