9

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?

Charles Jr
  • 8,333
  • 15
  • 53
  • 74

1 Answers1

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
  • i think, if change alreadyAdded type to Array, it will be faster. – masakih Mar 27 '17 at 15:57
  • @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
  • @masakih `insert(_:)` is also constant time for sets, too. – Alexander Mar 28 '17 at 03:35
  • 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
  • @masakih Try an array with 100,000 elements, see how that goes – Alexander Jul 12 '17 at 17:46