Created
May 30, 2023 13:37
-
-
Save joaomvfsantos/6db15d82045b0a7854073e21e7cac673 to your computer and use it in GitHub Desktop.
Swift Array extension that removes duplicate elements, provided the elements conform to the Equatable protocol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RemoveDuplicates.swift | |
// | |
// Created by João Santos on 25/05/2020. | |
// Copyright © 2020 João Santos. All rights reserved. | |
// | |
import Foundation | |
extension Array where Element: Equatable { | |
func removingDuplicates() -> [Element] { | |
var addedDict = [Element]() | |
return filter { | |
if addedDict.contains($0) { | |
return false | |
} | |
addedDict.append($0) | |
return true | |
} | |
} | |
mutating func removeDuplicates() { | |
self = self.removingDuplicates() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment