To remove duplicates from an array in Swift, you can use the `Set` type or a loop with a `contains` check.
In this example,
let numbers = [1, 2, 2, 3, 4, 4, 5]
let uniqueNumbers = Array(Set(numbers))
print("Array without duplicates:", uniqueNumbers)
Array without duplicates: [5, 2, 3, 1, 4]
In this example,
let colors = ["red", "green", "blue", "green", "yellow", "blue"]
var uniqueColors = [String]()
for color in colors {
if !uniqueColors.contains(color) {
uniqueColors.append(color)
}
}
print("Array without duplicates:", uniqueColors)
Array without duplicates: ["red", "green", "blue", "yellow"]
In this tutorial, we learned How to Remove Duplicates in an Array in Swift language with well detailed examples.