Remove duplicates

bearophile bearophileHUGS at lycos.com
Tue May 21 15:00:07 PDT 2013


Sometimes I have need a simple function like this, related to 
std.string.squeeze:


// Must keep the original order of the items.
// Slow implementation that shows the semantics.
T[] noDupes(T)(in T[] s) {
     import std.algorithm: canFind;
     T[] result;
     foreach (T c; s)
         if (!result.canFind(c))
             result ~= c;
     return result;
}

void main() {
     import std.string: squeeze;
     assert("AAAA".noDupes == "A");
     assert("AAAA".squeeze == "A");
     assert("ABAC".noDupes == "ABC");
     assert("ABAC".squeeze == "ABAC");
}


Do you know if this function (or a simple way to implement it) 
already in Phobos?

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list