Home
Swift
Array Remove Duplicates
This page was last reviewed on Aug 23, 2023.
Dot Net Perls
Remove duplicates. An 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.
Duplicate strings. This example introduces a removeDuplicates method that receives a string array. It returns another string array.
Detail The "encountered" set is used to record strings that have been seen. We use contains to see if a string has been added.
Result This starts as an empty string array. We add elements with append() as we proceed in the for-in loop.
Thus We only add elements that have not been added to the set. We 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"]
Ints. 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.
Here We use the set initialization func to make a set of unique elements from the Int array.
Result The set converted back into an array. Only unique elements are in this array.
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]
Filter, count elements. 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.
Here We filter out all elements that do not equal "red" in the array. We have two strings in the result, so there is a duplicate "red."
Note This code does not remove duplicates. But it can be used to detect or report errors on dupes.
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.
Dictionary
Notes, Swift 3. 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.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 23, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.