If your intent is to just check if a name occurs in more than one collection I think the best way to approach this is creating a single collection with all the names and filter the duplicates as shown in this post
let array1 = ["Max", "Peter","Kathrin", "Sara", "Kirsten", "Mike", "Elon"]
let array2 = ["Pamela", "Chris", "James", "Sebastian", "Mike"]
let array3 = ["John", "Daniel", "Susan", "Mathias", "Mike", "Donald"]
let array4 = ["Tim", "Kathrin", "Alan", "Chris", "Amy", "Sara"]
let array5 = ["Cara", "Charly", "Emily", "Maja", "Peter", "Sara"]
var names: [String] = []
names.append(contentsOf: array1)
names.append(contentsOf: array2)
names.append(contentsOf: array3)
names.append(contentsOf: array4)
names.append(contentsOf: array5)
extension RangeReplaceableCollection where Element: Hashable {
var duplicates: Self {
var set: Set<Element> = []
var filtered: Set<Element> = []
return filter { !set.insert($0).inserted && filtered.insert($0).inserted }
}
}
// Output should be: Peter, Kathrin, Mike, Sara, Chris
print(names.duplicates) // ["Mike", "Kathrin", "Chris", "Sara", "Peter"]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…