I have two arrays, I am trying to check if contents of one is present in the other array. However, I got a crash with error
Thread 1: Fatal error: Duplicate elements of type 'Report' were found in a Set.
This usually means either that the type violates Hashable's requirements, or
that members of such a set were mutated after insertion.
this is what I am using to check
extension Array where Element: Hashable {
func set() -> Set<Array.Element> {
return Set(self)
}
func isSubset(of array: Array) -> Bool {
self.set().isSubset(of: array.set())
}
func isSuperset(of array: Array) -> Bool {
self.set().isSuperset(of: array.set())
}
func commonElements(between array: Array) -> Array {
let intersection = self.set().intersection(array.set())
return intersection.map({ $0 })
}
func hasCommonElements(_ array: Array) -> Bool {
return self.commonElements(between: array).count >= 1 ? true : false
}
}
How can I correct this or if there is an alternative.
I have a variable allTransactionHistory
which is of type [Report]
variable reports is new data fetched from an API which is also of type [Report]
allTransactionHistory.hasCommonElements(reports)
struct Report: Hashable {
}