liveBook · Manning

Distinct value

As explained in The Well-Grounded Rubyist, Fourth Edition

uniq is a Ruby Array method that returns a new array with duplicate elements removed according to equality comparison.

Array#uniq returns a new array with duplicate elements removed. It determines duplicates by comparing pairs of elements with ==; uniq! removes duplicates from the original array. section 9.2.4

>> [1,2,3,1,4,3,5,1].uniq
=> [1, 2, 3, 4, 5]

Repeated integers are reduced to one occurrence each in the returned array. section 9.2.4

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines duplicates through pairwise == tests and notes uniq! as the in-place counterpart.
Examples
Demonstrates removing repeated integers from an array, yielding [1, 2, 3, 4, 5].
What you can do after
Apply Array#uniq to return a deduplicated array, or use uniq! to remove duplicates from the original.
How it connects
Ties array transformation to Ruby's == equality comparison.

Also explained in

Compare how 3 books explain distinct value →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.