Removing duplicate elements from an array in Swift(在 Swift 中从数组中删除重复元素)
问题描述
I might have an array that looks like the following:
Or, really, any sequence of like-typed portions of data. What I want to do is ensure that there is only one of each identical element. For example, the above array would become:
Notice that the duplicates of 2, 6, and 15 were removed to ensure that there was only one of each identical element. Does Swift provide a way to do this easily, or will I have to do it myself?
You can roll your own, e.g. like this:
And as an extension for Array
:
Or more elegantly (Swift 4/5):
Which would be used:
这篇关于在 Swift 中从数组中删除重复元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!