Removing duplicate elements from an array in Swift

Altair357 picture Altair357 · Sep 9, 2014 · Viewed 171.8k times · Source

I might have an array that looks like the following:

[1, 4, 2, 2, 6, 24, 15, 2, 60, 15, 6]

Answer

Ben Packard picture Ben Packard · Apr 27, 2015

You can convert to a Set and back to an Array again quite easily:

let unique = Array(Set(originals))

This is not guaranteed to maintain the original order of the array.