Sequence

extension Sequence
  • Returns a sequence with duplicate elements removed, performing the comparison usinig the property at the supplied keypath.

    i.e.

    [
      MyStruct(value: "Hello"),
      MyStruct(value: "Hello"),
      MyStruct(value: "World")
     ].uniqued(\.value)
    

    would result in

    [
      MyStruct(value: "Hello"),
      MyStruct(value: "World")
    ]
    

    Note

    Source: stackoverflow.com/a/46354989/3141234

    Declaration

    Swift

    public func unique<T>(by keyPath: KeyPath<Element, T>) -> [Element] where T : Equatable
  • Return the sequence with all duplicates removed.

    Duplicate, in this case, is defined as returning true from comparator.

    Note

    Source: stackoverflow.com/a/46354989/3141234

    Declaration

    Swift

    public func unique(by comparator: @escaping (Element, Element) throws -> Bool) rethrows -> [Element]

    Parameters

    comparator

    A comparison closure. comparator accepts two elements of this sequence as its parameter and returns a truthy boolean if the two elements have the same identifier

    Return Value

    A sequence containing only elements uniquely identified by the given comparator