I have a formula retuning an array as example var Array = [a,s,d,s,f,g,g,h,e]
. What I want is to run a for
loop or some other option that gives me back a,s,d,f,g,h,e
- only the unique values. How can I do this with ios Swift?
Asked
Active
Viewed 8,278 times
9

Charles Jr
- 8,333
- 15
- 53
- 74
-
Do you care about the order of the elements? – Alexander Nov 13 '16 at 23:17
1 Answers
18
If you don't care about order:
Simply use a set:
let set: Set = ["a", "s", "d", "s", "f", "g" , "g", "h", "e"]
print(set) // ["a", "s", "f", "g", "e", "d", "h"]
If you care about the order:
Use this extension, which allows you to remove duplicate elements of any Sequence
, while preserving order:
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", "s", "d", "s", "f", "g" , "g", "h", "e"]
let result = array.unique()
print(result) // ["a", "s", "d", "f", "g", "h", "e"]

Alexander
- 59,041
- 12
- 98
- 151
-
-
@masakih What makes you suspect that? Sets have `O(1)` look ups (needed for fast `contains(_:)` calls), whereas array have O(n) loop ups in the general case – Alexander Mar 27 '17 at 16:44
-
sorry! i was focusing on `insert(_:)` only. but i had overlooked `contains(_:)`. – masakih Mar 28 '17 at 03:04
-
-
i checked which fast are `Array.append(_:)` and `Set.insert(_:)`. `Array.append(_:)` uses half time of `Set.insert(_:)`. of course, in this case Set is best. [https://swift.sandbox.bluemix.net/#/repl/58d9e75c50cb957f193a78d9] – masakih Mar 28 '17 at 04:41
-
I never said that they're the same speed. I just said that they're both `O(1)`. – Alexander Mar 28 '17 at 04:46
-