Search code examples
rubyenumerable

Ruby removing duplicates in enumerable lists


Is there a good way in ruby to remove duplicates in enumerable lists (i.e. reject, etc.)


Solution

  • For array you can use uniq() method

    a = [ "a", "a", "b", "b", "c" ]
    a.uniq   #=> ["a", "b", "c"]
    

    so if you just

    (1..10).to_a.uniq
    

    or

    %w{ant bat cat ant}.to_a.uniq
    

    because anyway almost every methods you do implement will return as an Array class.