4

Scenario: An array of strings, many are duplicated.

Goal: Produce a UNIQUE array of strings.

Modus Operandi: I was thinking of converting the array to a set of strings which become unique; from which to generate a new array of unique strings.

Question: How does one convert a Swift array into a Swift Set?

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
  • See http://stackoverflow.com/questions/25738817 or http://stackoverflow.com/questions/27624331 – Rob Dec 22 '15 at 01:55

2 Answers2

7

Have you tried let myset = Set(myarray) ?

Chris Dennett
  • 22,412
  • 8
  • 58
  • 84
7
let nonUniqueArray = ["A", "B", "C", "C", "B", "A"]
let uniqueArray = Array(Set(nonUniqueArray))
print(uniqueArray)

produces

["C", "B", "A"]

Swift 2.2 produces exactly the same result as well.

timbo
  • 13,244
  • 8
  • 51
  • 71