Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 2.1: How do you produce a set from an array?

Tags:

arrays

set

swift2

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?

like image 599
Frederick C. Lee Avatar asked Dec 18 '22 21:12

Frederick C. Lee


1 Answers

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.

like image 75
timbo Avatar answered Jan 08 '23 17:01

timbo