How do I check an array for duplicates? [duplicate]

Just call uniq on it (which returns a new array without duplicates) and see whether the uniqed array has less elements than the original:

if a.uniq.length == a.length
  puts "a does not contain duplicates"
else
  puts "a does contain duplicates"
end

Note that the objects in the array need to respond to hash and eql? in a meaningful for uniq to work properly.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)