-
Notifications
You must be signed in to change notification settings - Fork 78
/
DistinctBy.swift
29 lines (23 loc) · 940 Bytes
/
DistinctBy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// - distinct(by:) function
struct Student {
let id: Int
}
let students: [Student] = [1, 2, 2, 3, 3, 3].map({ Student(id: $0) })
// contents = student1, student2, student2, student3, student3, student3
let uniqueStudents = students.distinct(by: { $0.id }) // Distinct by id.
// contents = student1, student2, student3
// - Definition
public extension Sequence {
/// Returns a sequence that contains no duplicate entries according to the
/// generic hash and equality comparisons on the keys returned by the given
/// key-generating block. If an element occurs multiple times in the sequence
/// then the later occurrences are discarded.
///
/// - Parameter keyBlock: Key generating block.
public func distinct<Key: Hashable>(by keyBlock: (Iterator.Element) -> Key) -> [Iterator.Element] {
var seen: [Key: Bool] = [:]
return self.filter {
seen.updateValue(true, forKey: keyBlock($0)) == nil
}
}
}