Remove duplicate elements from an array in Swift

In this tutorial, we are going to learn how to remove duplicate elements from an array in Swift.

There are many ways to remove duplicates from an array. Here, we will discuss the most effective and easiest methods in a simple manner. Those are:

  • Using an empty array (maintains order)
  • Using an empty set (doesn’t maintain order)
  • Using an extension to array ( maintains order )

Let us discuss each of them in detail.

Using an empty array

Firstly, we will declare an empty array of the desired type. Then by iterating through our actual array, we will append the elements to the empty array if that element doesn’t exist before in it.

Program to remove duplicates using an empty array

var arr = [Int]()
var dup=[1,2,3,4,4,4,1,5,4,6,7,6]
for i in dup{
    if !arr.contains(i){
        arr.append(i)
    }
}
print(arr)

Output:

[1, 2, 3, 4, 5, 6, 7]

In the above code, we iterated through elements of our actual array dup and checked whether the array arr has those elements or not. If the array arr doesn’t contain the elements, then it appends the element to it. Otherwise, it skips. As a result, the final arr array contains the unique elements without changing the order.

Even though we get the elements in order, this method has a time complexity of O(N²) i.e we are iterating over N elements using for loop which takes O(N) and searching N elements using contains() method that takes O(N) contributing to a total of O(N²). Hence, this is ineffective.

Using an empty set

Similar to the above method, we will just create an empty set instead of an empty array and everything else is the same.

Program to remove duplicates using an empty set

var s: Set<Int>=[]
var dup=[1,2,3,4,4,4,1,5,4,6,7,6]
for i in dup{
    if !s.contains(i){
        s.insert(i)
    }
}
print(s)

Output:

[2, 4, 5, 6, 7, 3, 1]

In the above code, we declared an empty set s. Sets usually have a constant time complexity O(1) while searching for an element in it. Since we are iterating over N elements and searching each of them in a set we together have O(N) time complexity.
Even though this is an effective way, the resulting set will be in an unordered manner.

Using an extension to an array

In the above methods, we have seen if a method is effective, it doesn’t maintain the order and if a method maintains order, it is ineffective. In order to achieve both of these, we will create an extension to an array using the Hashable protocol.

Here, will use both the array and set to filter out the duplicates from an array by maintaining the order.

Program to remove duplicates using an extension to an array

extension Array where Element: Hashable {
    func uniquelements() -> Array {
        var temp = Array()
        var s = Set<Element>()
        for i in self {
            if !s.contains(i) {
                temp.append(i)
                s.insert(i)
            }
        }
        return temp
    }
}
var a=[1,2,3,4,4,4,1,5,4,6,7,6]
var x = a.uniquelements()
print(x)

Output:

[1, 2, 3, 4, 5, 6, 7]

In the above code, we used the Hashable protocol that matches the given element with all the existing elements. So while iterating over the elements of the array, contains() method checks whether the element does exist or not in the set in order of the given array. As a result, the overall time complexity of this method is O(N) which is most effective and moreover, this method maintains the order.

That’s it! Hope you understood how to remove duplicates from an array in Swift.

Also read,

Leave a Reply

Your email address will not be published.