Google 그룹스는 더 이상 새로운 유즈넷 게시물 또는 구독을 지원하지 않습니다. 과거의 콘텐츠는 계속 볼 수 있습니다.

Making a count of unique elements in an array

조회수 1,795회
읽지 않은 첫 메시지로 건너뛰기

Dan Kohn

읽지 않음,
2006. 1. 9. 오전 1:47:1606. 1. 9.
받는사람
Given an array, I need to produce a two-dimensional resulting array
where each pair consists of a unique element from the original array
and the number of times that element appears.

I found two ways to do this. Is one of them better? Is there a better
way?

arr = %w{a b b c c d e e e}
p arr
p arr.uniq.map {|e| [e, (arr.select {|ee| ee == e}).size ]}
counter = {}
arr.each {|e| counter[e] += 1 rescue counter[e] = 1 }
p counter.to_a


outputs:

["a", "b", "b", "c", "c", "d", "e", "e", "e"]
[["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]
[["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]

Peña, Botp

읽지 않음,
2006. 1. 9. 오전 2:07:4706. 1. 9.
받는사람
Dan Kohn [mailto:d...@dankohn.com] wrote:

#counter = {}
#arr.each {|e| counter[e] += 1 rescue counter[e] = 1 }
#p counter.to_a

me, like the above but w slight modif below:

irb(main):013:0> counter2 = Hash.new(0)
=> {}
irb(main):014:0> arr.each {|e| counter2[e] += 1 }
=> ["a", "b", "b", "c", "c", "d", "e", "e", "e"]
irb(main):015:0> counter2.to_a
=> [["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]
irb(main):016:0>

(note the plain counter and the hash param)

hth.

kind regards -botp


Robert Klemme

읽지 않음,
2006. 1. 9. 오전 5:07:5806. 1. 9.
받는사람

I like #inject 1-liners:

>> arr = %w{a b b c c d e e e}

=> ["a", "b", "b", "c", "c", "d", "e", "e", "e"]
>> arr.inject(Hash.new(0)) {|h,x| h[x]+=1;h}
=> {"a"=>1, "b"=>2, "c"=>2, "d"=>1, "e"=>3}
>> arr.inject(Hash.new(0)) {|h,x| h[x]+=1;h}.to_a
=> [["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]
>> arr.inject(Hash.new(0)) {|h,x| h[x]+=1;h}.sort
=> [["a", 1], ["b", 2], ["c", 2], ["d", 1], ["e", 3]]

:-)

Kind regards

robert

vsing...@gmail.com

읽지 않음,
2013. 12. 12. 오후 12:56:2813. 12. 12.
받는사람
if i have to count no. of distinct elements in a 2D array ,then what to do??

Norbert Melzer

읽지 않음,
2013. 12. 12. 오후 1:44:4713. 12. 12.
받는사람
On 12.12.2013 18:56, vsing...@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

Robert Klemme

읽지 않음,
2013. 12. 14. 오전 11:22:0313. 12. 14.
받는사람
On 12.12.2013 18:56, vsing...@gmail.com wrote:
> if i have to count no. of distinct elements in a 2D array ,then what to do??

Iterate and count. For counting you can use a Hash with default value 0.

Cheers

robert


lukea...@gmail.com

읽지 않음,
2013. 12. 26. 오후 4:44:2313. 12. 26.
받는사람
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]}

Robert Klemme

읽지 않음,
2013. 12. 28. 오전 6:02:4113. 12. 28.
받는사람
Downside of that approach is that it does unnecessary work and uses more
memory than necessary. And the result is not a Hash which may make
retrieval of the counts of an element quite expensive if there are many
different values.

Btw, the OP spoke of a "2D array". So the input likely looks different
than in your example. We also do not know what he actually wants to
count. Is it individual elements? Or does "distinct elements" refer to
a single array?

Kind regards

robert

새 메시지 0개