Forked from krzyzanowskim/remove_duplicates_reduce
Created
September 22, 2014 20:31
-
-
Save ColinEberhardt/3908cc252b823e449510 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let array = ["P","Q","R","S","T","P","R","A","T","B","C","P","P","P","P","P","C","P","P","J"] | |
extension Array { | |
func unique<T: Equatable>() -> [T] { | |
return self.reduce([T](), combine: { (array, value) -> [T] in | |
var result = array | |
let valAsT = value as T | |
if (!contains(array, valAsT)) { | |
result.append(valAsT) | |
} | |
return result | |
}) | |
} | |
} | |
let unique: [String] = array.unique() | |
println() // [P, Q, R, S, T, A, B, C, J] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment