Home

Search

Swift Remove Duplicates From ArrayUse funcs, and the filter method, to remove duplicate elements from arrays.
dot net perls

Remove duplicates. An array contains many elements. But some of these are 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.

Set 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.

Swift program that removes duplicates from string array
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.

Set 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

Swift program that removes duplicate integers
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.

Swift program that filters elements
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.

Home
© 2007-2021 sam allen. send bug reports to info@dotnetperls.com.