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


Groups > comp.lang.ruby > #6897

Re: Making a count of unique elements in an array

From Norbert Melzer <norbert.melzer@gmx.net>
Newsgroups comp.lang.ruby
Subject Re: Making a count of unique elements in an array
Date 2013-12-12 19:44 +0100
Message-ID <bgueh0FdoqiU1@mid.individual.net> (permalink)
References <1136789236.482098.144200@o13g2000cwo.googlegroups.com> <5327790f-e3da-4cb6-a94c-b198de7139da@googlegroups.com>

Show all headers | View raw


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:
<http://www.ruby-doc.org/core-2.0.0/Array.html>

You can find nearly anything about arrays there…

HTH
Norbert

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