Swift Array: removing duplicate elements.

Recently I had to remove duplicate items from an Swift Array while maintaining the original order.

Searching on the web you can find many solutions but most of them retun a copy of the Array. According to Apple’s Swift guide:

If you create an array, a set, or a dictionary, and assign it to a variable, the collection that is created will be mutable. This means that you can change (or mutate) the collection after it is created by adding, removing, or changing items in the collection.

I didn’t want to create any sort of copy of the Array being not forced to use an immutable array. If we declare the array as a variable we can use the Mutating Keyword so we can directly remove duplicate elements from the Array.

So my prefered solutions was just the simplest one using an Array Extension.

I’d love to hear feedback if anyone has any better idea.

I hope you find this article useful. Thanks for reading!

Follow me on Twitter