A = [[1,2,3], [2,4,6], [3,6,2,1],[1,1,1],..].
A的大小是动态的。我想在A中找到共同点。如何在Swift中实现?
let lists = [[1,2,3], [2, 3, 4], [3, 4, 5]]
您可以找到书写的常见元素
let firstList = lists.first ?? [] let commonElements = lists.reduce(Set(firstList)) { (result, list) in result.intersection(list) }
结果
print(commonElements) // [3]