Forgive the contrived example below, but how can I filter like this? Using a Set
to dedup isn’t an option since my real data objects have another property that is unique for each.
struct MyDataObject {
var startDate: Date
var endDate: Date
}
let dataObject1 = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let duplicateDataObject = MyDataObject(startDate: Date().startOfDay(), endDate: Date().startOfDay())
let array = [dataObject1, duplicateDataObject]
//How to filter to end up with an array of data objects with a unique start date?
2
Answers
https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md
Or, if you need more control of which elements get chosen:
Do you mean filter so you only keep elements that appear exactly once in the original collection?
Gives just the A’s with unique id’s (of 3 and 4 in this example)
Or do you mean just to deduplicate, like https://github.com/apple/swift-algorithms/blob/main/Guides/Unique.md
uniqued(on:)
and if so which element should be retained if there are more than one, the first seen, the last etc?