参见英文答案 >
removing duplicate elements from an array34个
> Swift 3 Generics: How to Find The Common Set of Two Generic Arrays2个
我有一个公式重新调整数组作为例子var Array = [a,s,d,f,g,h,e].我想要的是运行一个for循环或一些其他选项,它给我一个,e – 只有唯一值.我怎么能用ios Swift做到这一点?
> Swift 3 Generics: How to Find The Common Set of Two Generic Arrays2个
我有一个公式重新调整数组作为例子var Array = [a,s,d,f,g,h,e].我想要的是运行一个for循环或一些其他选项,它给我一个,e – 只有唯一值.我怎么能用ios Swift做到这一点?
如果您不关心订单:
原文链接:https://www.f2er.com/swift/319950.html只需使用一套:
let set: Set = ["a","s","d","f","g","h","e"] print(set) // ["a","e","h"]
如果您关心订单:
使用此扩展,允许您删除AnySequence的重复元素,同时保留顺序:
extension Sequence where Iterator.Element: Hashable { func unique() -> [Iterator.Element] { var alreadyAdded = Set<Iterator.Element>() return self.filter { alreadyAdded.insert($0).inserted } } } let array = ["a","e"] let result = array.unique() print(result) // ["a","e"]