Last active
August 15, 2019 13:12
-
-
Save wbbernardes/2a4f4c0301ac5c872cfa2bfa001211e0 to your computer and use it in GitHub Desktop.
Remove duplicate elemente in array
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
// | |
// Array+RemoveDuplicate.swift | |
// | |
// Created by Wesley Brito on 12/08/19. | |
// Copyright © 2019 Wesley Brito. All rights reserved. | |
// | |
import Foundation | |
extension Array where Element: Hashable { | |
func removingDuplicates() -> [Element] { | |
var addedDict = [Element: Bool]() | |
return filter { | |
addedDict.updateValue(true, forKey: $0) == nil | |
} | |
} | |
mutating func removeDuplicates() { | |
self = self.removingDuplicates() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment