Skip to content

Instantly share code, notes, and snippets.

@hanachin
Forked from kkosuge/gist:2928806
Created June 14, 2012 08:04
Show Gist options
  • Save hanachin/2928908 to your computer and use it in GitHub Desktop.
Save hanachin/2928908 to your computer and use it in GitHub Desktop.
配列に含まれている数量が多い順番にソートして重複した要素を取り除く[Ruby]

配列に含まれている数量が多い順番にソートして重複した要素を取り除く[Ruby]

p [1,2,3,3,3,3,3,4,4,5,5,5].instance_eval { uniq.sort_by {|x| count x}.reverse }

#=> [3, 5, 4, 2, 1]

他になんかいい方法とかより良いメソッド名があったら教えてくださひ

第2弾!知って得する12のRubyのトリビアな記法見て書き直した。 instance_eval便利。

@melborne
Copy link

group_by{|e| e} が正解ですね。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment