Skip to content

Instantly share code, notes, and snippets.

@mikekavouras
Created April 11, 2016 20:18
Show Gist options
  • Save mikekavouras/ff1eaea47585cb477860dd63919ac1af to your computer and use it in GitHub Desktop.
Save mikekavouras/ff1eaea47585cb477860dd63919ac1af to your computer and use it in GitHub Desktop.
uni
//: Playground - noun: a place where people can play
import UIKit
struct Obj {
let id: String
init(id: String) {
self.id = id
}
}
extension Obj: Equatable {}
func ==(lhs: Obj, rhs: Obj) -> Bool {
return lhs.id == rhs.id
}
extension Array where Element: Equatable{
var unique: [Element] {
var uniqueValues: [Element] = []
forEach { item in
if !uniqueValues.contains(item) {
uniqueValues += [item]
}
}
return uniqueValues
}
}
let one = Obj(id: "beep")
let two = Obj(id: "beep")
let three = Obj(id: "boop")
var arr = [one, two, three]
var uniq: [Obj] = arr.unique
print(uniq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment