Swift Program to Remove Duplicate Elements From an Array


In this article, we will learn how to write a swift program to remove duplicate elements from an array. As we know that an array can store multiple elements of the same type. It is not necessary that all the elements are unique they can be duplicated. For example, arr = [3, 5, 6, 3, 7, 8, 3, 7, 7] here 3 and 7 are duplicate elements.

So here we use the following methods to remove duplicate elements −

  • Using contains() method

  • Using Set() initializer

Method 1: Using contains() method

To remove duplicate elements from the array we iterate through all the elements of the original array and check if the new array contains the specified element from the original array using contains() method. If not, then we add that element to the new array using the append() method.

Algorithm

Step 1 − Create a function.

Step 2 − In this function, create an empty array to store the unique elements.

Step 3 − Use for loop to iterate through each element of the array.

Step 4 − check for the unique elements using contains() function and store the unique elements in the array using the append() function.

Step 5 − Create an array of integer types.

Step 6 − Call the function and pass the created array as a parameter in it.

Step 7 − Print the output.

Example

Following the Swift program to remove duplicate elements from an array.

import Foundation
import Glibc

// Function to remove duplicate elements from the given array
func removeDuplicateElements(arr: [Int]) -> [Int] {
   var uniqueElements: [Int] = []
   for x in arr {
      if !uniqueElements.contains(x) {
         uniqueElements.append(x)
      }
   }
   return uniqueElements
}
let myArray = [3, 5, 67, 5, 32, 3, 5, 6, 3, 2, 1]
print("Original Array:", myArray)
print("Modified array:", removeDuplicateElements(arr: myArray)) 

Output

Original Array: [3, 5, 67, 5, 32, 3, 5, 6, 3, 2, 1]
Modified array: [3, 5, 67, 32, 6, 2, 1]

Here in the above code, we have an array of integer types. Now we create a function to remove duplicate elements. In this function, we iterate through each element and check if the given value is present in the uniqueElements array or not using contains() method. If not, then add that value to the uniqueElements array using append(). So at the end of the for loop uniqueElements array contains the unique elements from the original array.

Method 2: Using Set() Initializer

To remove duplicate elements from the array we convert the given array into a set using the Set() constructor and then convert it back into an array using Array() constructor. Here we convert it into a set because set type always contains unique elements.

Algorithm

Step 1 − Create an array.

Step 2 − Convert the array into a set using the Set() initializer and convert it back into an array using Array() initializer.

Step 3 − Store the result in a new array.

Step 4 − Print the output.

Example

Following Swift program to remove duplicate elements from an array.

import Foundation
import Glibc

// Array of integer type
let Num = [11, 22, 2, 31, 3, 31, 41, 4, 41, 4]

// Finding unique elements using set
let uniqueNumbers = Array(Set(Num))
print("Original Array:", Num)
print("Modified array:", uniqueNumbers)

// Array of string type
let myArray = ["Ox", "Cow", "Bee", "Cow", "Ox", "Cat"]

// Finding unique elements using set
let uniqueElements = Array(Set(myArray))
print("\nOriginal Array:", myArray)
print("Modified array:", uniqueElements) 

Output

Original Array: [11, 22, 2, 31, 3, 31, 41, 4, 41, 4]
Modified array: [3, 2, 11, 41, 31, 22, 4]

Original Array: ["Ox", "Cow", "Bee", "Cow", "Ox", "Cat"]
Modified array: ["Cow", "Cat", "Bee", "Ox"]

Here in the above code, we have an array of integer types. Now we convert the given array into a set using the Set() initializer and convert the result back into an array using the Array() initializer to remove the duplicate elements from the given array.

Conclusion

Therefore, this is how we can remove duplicate elements from the given array. Both are effective methods to remove duplicates.

Updated on: 09-Jan-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements