2

How to remove duplicate items from Array in Swift

 1 year ago
source link: https://sarunw.com/posts/remove-duplicate-items-from-array-in-swift/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In Swift, there are many ways to remove duplicate items from an Array.

In this article, I will show you two of them.

Using Swift Algorithms module

In WWDC 2021, Apple announced two new Swift open sources, Swift Algorithms and Swift Collections. A stand-alone Swift package you can add to your project.

This Swift Algorithms package contains a uniqued() method which removes duplicate items from an Array.

The only requirement is the element in an array must conform to the Hashable protocol.

To use it, you need to add the Swift Algorithms package from https://github.com/apple/swift-algorithms.

Then use it like this.

import Algorithms

let numbers = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
let uniquedNumbers = numbers.uniqued()

The return type of uniqued() is UniquedSequence, which conforms to LazySequenceProtocol.

You might need to use an Array initializer to use it as an array.

Array(uniquedNumbers)
// [1, 2, 3, 4]

Custom Array extension

If you don't want to bother adding a Swift package for this, you can implement it with a few lines of code.

There are many ways to do this, but I choose a quite popular one as an example here.

public extension Array where Element: Hashable {
func uniqued() -> [Element] {
var seen = Set<Element>()
return filter { seen.insert($0).inserted }
}
}

Explanation

What this implementation is trying to do is.

  1. We go through every element in the array.
  2. If we haven't seen this element before, we add it to the new array.
  3. If we have seen the element, it is considered a duplicate. We won't add this to the new array.
public extension Array where Element: Hashable {
func uniqued() -> [Element] {
// 1
var seen = Set<Element>()

// 2
return self.filter { element in
// 3
let (inserted, memberAfterInsert) = seen.insert(element)
return inserted
}
}
}

1 We create a Set to store an element that we have already seen (include in a new array).
2 We go through every element using filter(_:).

  • If an element hasn't seen before, we return true to include that element in the new array.
  • If we have seen the element, we return false and not include it in the new array.

We use the insert(_:) method to determine whether an element has been seen or not.

insert(_:) method inserts the given element in the set if it is not already present. We can get this result from the first element of the returning Tuple.

(inserted: Bool, memberAfterInsert: Element)
  • inserted equals true means the element is not already seen.
  • inserted equals false means the element is already in the Set.

Since the inserted dictate whether we haven't seen the element before, we can use the inserted to filter our array 3.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK