Skip to content

Instantly share code, notes, and snippets.

@joaomvfsantos
Created May 30, 2023 13:37
Show Gist options
  • Save joaomvfsantos/6db15d82045b0a7854073e21e7cac673 to your computer and use it in GitHub Desktop.
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
//
// 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