15
15
16
16
extension Sequence where Element: Hashable {
17
17
/// Returns an array with only the unique elements of this sequence, in the
18
- /// order of the first occurence of each unique element.
18
+ /// order of the first occurrence of each unique element.
19
+ ///
20
+ /// let animals = ["dog", "pig", "cat", "ox", "dog", "cat"]
21
+ /// let uniqued = animals.uniqued()
22
+ /// print(uniqued)
23
+ /// // Prints '["dog", "pig", "cat", "ox"]'
24
+ ///
25
+ /// - Returns: An array with only the unique elements of this sequence.
26
+ /// .
27
+ /// - Complexity: O(*n*), where *n* is the length of the sequence.
19
28
@inlinable
20
29
public func uniqued( ) -> [ Element ] {
21
30
uniqued ( on: { $0 } )
@@ -24,8 +33,26 @@ extension Sequence where Element: Hashable {
24
33
25
34
extension Sequence {
26
35
/// Returns an array with the unique elements of this sequence (as determined
27
- /// by the given projection), in the order of the first occurence of each
36
+ /// by the given projection), in the order of the first occurrence of each
28
37
/// unique element.
38
+ ///
39
+ /// This example finds the elements of the `animals` array with unique
40
+ /// first characters:
41
+ ///
42
+ /// let animals = ["dog", "pig", "cat", "ox", "cow", "owl"]
43
+ /// let uniqued = animals.uniqued(on: {$0.first})
44
+ /// print(uniqued)
45
+ /// // Prints '["dog", "pig", "cat", "ox"]'
46
+ ///
47
+ /// - Parameter projection: A closure that transforms an element into the
48
+ /// value to use for uniqueness. If `projection` returns the same value
49
+ /// for two different elements, the second element will be excluded
50
+ /// from the resulting array.
51
+ ///
52
+ /// - Returns: An array with only the unique elements of this sequence, as
53
+ /// determined by the result of `projection` for each element.
54
+ ///
55
+ /// - Complexity: O(*n*), where *n* is the length of the sequence.
29
56
@inlinable
30
57
public func uniqued< Subject: Hashable > (
31
58
on projection: ( Element ) throws -> Subject
0 commit comments