Unique Elements : uniq « Array « Ruby






Unique Elements


# uniq method removes duplicates from a single array, creating a new array. 
# uniq! changes the array itself, in place.


shopping_list = %w[ cheese bread crackers potatoes carrots cheese ]
# => ["cheese", "bread", "crackers", "potatoes", "carrots", "cheese"]

shopping_list.uniq!=> ["cheese", "bread", "crackers", "potatoes", "carrots"]

 








Related examples in the same category

1.Call uniq to remove the duplications
2.Stripping Duplicate Elements from an Array