Skip to main content
Get rid of intermediate variable (inspired by Alexander's comment)
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: KeyPath<Element, T>)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = value[keyPath: key]
            if seen.insert(keyvalue[keyPath: key]).inserted {
                result.append(value)
            }
        }
        return result
    }
}
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: (Element) -> T)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = key(value)
            if seen.insert(key(value)).inserted {
                result.append(value)
            }
        }
        return result
    }
}
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: KeyPath<Element, T>)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = value[keyPath: key]
            if seen.insert(key).inserted {
                result.append(value)
            }
        }
        return result
    }
}
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: (Element) -> T)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = key(value)
            if seen.insert(key).inserted {
                result.append(value)
            }
        }
        return result
    }
}
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: KeyPath<Element, T>)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            if seen.insert(value[keyPath: key]).inserted {
                result.append(value)
            }
        }
        return result
    }
}
extension Array {
    func removingDuplicates<T: Hashable>(byKey key: (Element) -> T)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            if seen.insert(key(value)).inserted {
                result.append(value)
            }
        }
        return result
    }
}
added 648 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

Another (more flexible) option is to pass a closure to the function which determines the uniquing key for each element:

extension Array {
    func removingDuplicates<T: Hashable>(byKey key: (Element) -> T)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = key(value)
            if seen.insert(key).inserted {
                result.append(value)
            }
        }
        return result
    }
}

Example:

let withoutDuplicates = searchResults.removingDuplicates(byKey: { $0.index })

Another (more flexible) option is to pass a closure to the function which determines the uniquing key for each element:

extension Array {
    func removingDuplicates<T: Hashable>(byKey key: (Element) -> T)  -> [Element] {
        var result = [Element]()
        var seen = Set<T>()
        for value in self {
            let key = key(value)
            if seen.insert(key).inserted {
                result.append(value)
            }
        }
        return result
    }
}

Example:

let withoutDuplicates = searchResults.removingDuplicates(byKey: { $0.index })
added 104 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96

First note that there is no need to make the SearchResult properties implicitly unwrapped(implicitly unwrapped) optionals, as they are always initialized:

Note that if seen.insert(key).inserted does the “test and insert if not present” with a single call.

First note that there is no need to make the SearchResult properties implicitly unwrapped optionals, as they are always initialized:

First note that there is no need to make the SearchResult properties (implicitly unwrapped) optionals, as they are always initialized:

Note that if seen.insert(key).inserted does the “test and insert if not present” with a single call.

deleted 32 characters in body
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading
Source Link
Martin R
  • 24.2k
  • 2
  • 38
  • 96
Loading