Ruby/Array/uniq
Материал из Wiki.crossplatform.ru
Call uniq to remove the duplications
shopping_list = %w[ cheese bread crackers potatoes carrots cheese ] shopping_list.uniq! # => ["cheese", "bread", "crackers", "potatoes", "carrots"]
Stripping Duplicate Elements from an Array
survey_results = [1, 2, 7, 1, 1, 5, 2, 5, 1] p distinct_answers = survey_results.uniq # => [1, 2, 7, 5] p survey_results.uniq! p survey_results # => [1, 2, 7, 5]
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"]