Remove
duplicatesAn array in Swift 5.8 may contain many elements, but some may be duplicates. With a set, or a for
-loop, we can eliminate duplicates.
When ordering is important, a method can be used to retain original ordering. If order does not matter, the Set initializer can be used.
This example introduces a removeDuplicates
method that receives a string
array. It returns another string
array.
string
has been added.string
array. We add elements with append()
as we proceed in the for-in
loop.append()
and insert()
those elements as we encounter them.func removeDuplicates(array: [String]) -> [String] { var encountered = Set<String>() var result: [String] = [] for value in array { if encountered.contains(value) { // Do not add a duplicate element. } else { // Add value to the set. encountered.insert(value) // ... Append the value. result.append(value) } } return result } // Test this array of animal names. let animals: [String] = ["bird", "cat", "cat", "fish", "bird"] print(animals) // Call the method to dedupe the string array. let dedupe = removeDuplicates(array: animals) print(dedupe)["bird", "cat", "cat", "fish", "bird"] ["bird", "cat", "fish"]
This method removes duplicate Ints from an array. Unlike the first example, it does not retain ordering. So it may result in a scrambled array.
func
to make a set of unique elements from the Int
array.func removeDuplicateInts(values: [Int]) -> [Int] { // Convert array into a set to get unique values. let uniques = Set<Int>(values) // Convert set back into an Array of Ints. let result = Array<Int>(uniques) return result } // Remove duplicates from these example numbers. let numbers: [Int] = [10, 20, 20, 30, 40, 50, 10] let uniqueNumbers = removeDuplicateInts(values: numbers) print(numbers) print(uniqueNumbers)[10, 20, 20, 30, 40, 50, 10] [50, 10, 20, 30, 40]
With filter, we can remove elements that do not match a value. This can be used to detect duplicate elements. We filter out other elements, and count the result.
let colors = ["red", "orange", "red", "blue"] // Use filter to get all elements by value. let filtered = colors.filter { (x) -> Bool in x == "red" } // Display results. print(filtered) // When this count is 1, we have a distinct element (no dupes). print(filtered.count)["red", "red"] 2
Often, the fastest way to remove duplicates is not to add them. We can use a dictionary to keep track of what is present in the array. And then reject any duplicates as they are detected.
For new versions of Swift, we must use a name for the arguments of functions when calling them. For methods like removeDuplicates
, a small syntax update is needed.
With these approaches, we eliminate duplicates from ("dedupe") an array. Performance of these approaches differs. And often the shortest, clearest code is a good choice.