Edited at

array.ununiq

More than 3 years have passed since last update.


uniq.rb

array = %w(1 1 2 3 4 5 5 6 7 8 9 9 9).map(&:to_i)

=> [1, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9, 9]

array.uniq
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]


重複はuniqメソッドで簡単に取り除ける。

じゃあ重複した値を確認したい時は?


ununiq.rb

array.select { |a| array.index(a) != array.rindex(a) }.uniq

=> [1, 5, 9]

既出の恐れが高い