After some amount of stress, I created the following generic function:
func removeDupes<T : Hashable > (inout inputCollection : [T] ) -> [T] {
var hashMap = [T : Bool]()
var output = 0
for thing in inputCollection {
if !hashMap[thing] {
hashMap[thing] = true
inputCollection[output++] = thing
}
}
while (inputCollection.count > output) {
inputCollection.removeLast()
}
return inputCollection
}
So when you do:
var names = ["Bob", "Carol", "Bob", "Bob", "Carol", "Ted", "Ted", "Alice", "Ted", "Alice"]
removeDupes(&names)
names will contain: ["Bob", "Carol","Ted", "Alice"]
Now I'd like to generically add "removeDupes" as an extension method on Array and I'm floundering with the syntax, since I have to constrain it to be an array of Hashable items.
I was hoping I could declare it like this:
extension Array {
func removeDupes< T : Hashable> () -> [T] {
return removeDupes(&self)
}
}
But I get the error:
Array is not convertible to '@lvalue inout $T5'
I suspect the answer is either "you idiot, do this..." or "you can't do it"
Which will it be? :-D