数组 – 如何使用Swift显示数组的唯一元素?
发布时间:2020-12-14 05:43:56 所属栏目:百科 来源:网络整理
导读:参见英文答案 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循环或一些其他选项,它给我一个
参见英文答案 >
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做到这一点?
如果您不关心订单:
只需使用一套: 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"] (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |