Arrayから重複する要素の配列を抜き出す
やり過ぎはダメだと言われても、やっぱり基底クラスの拡張は楽しい!
class Array def duplicated select{ |v| count(v) > 1}.uniq end end
使い方
a = [3, 5, 3, 4] => [3, 5, 3, 4] [15] pry(main)> a.duplicated => [3] [16] pry(main)> a = [3, 5, 3, 4, 5] => [3, 5, 3, 4, 5] [17] pry(main)> a.duplicated => [3, 5]
参考
[Ruby: How to find and return a duplicate value in array? – Stack Overflow][1]