Stripping Duplicate Elements from an Array : uniq « Array « Ruby





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]

 

Related examples in the same category

1.Unique Elements
2.Call uniq to remove the duplications