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
  • 8,358
  • 15
  • 62
  • 97
  • See http://stackoverflow.com/questions/25738817 or http://stackoverflow.com/questions/27624331 – Rob Dec 22 '15 at 01:55

2 Answers2

6

Have you tried let myset = Set(myarray) ?

Chris Dennett
  • 21,982
  • 8
  • 55
  • 84
5
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
  • 11,517
  • 6
  • 48
  • 62