uniqued(on:)
Returns an array with the unique elements of this sequence (as determined by the given projection), in the order of the first occurrence of each unique element.
func uniqued<Subject>(on projection: (Element) throws -> Subject) rethrows -> [Element] where Subject : Hashable
Parameters
- projection
A closure that transforms an element into the value to use for uniqueness. If
projection
returns the same value for two different elements, the second element will be excluded from the resulting array.
Returns
An array with only the unique elements of this sequence, as determined by the result of projection
for each element.
This example finds the elements of the animals
array with unique first characters:
let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
let uniqued = animals.uniqued(on: { $0.first })
print(uniqued)
// Prints '["dog", "pig", "cat", "ox"]'