D语言新手问题 去除重复的数组项

· Created · Last modified by 龙卷锋123 replied at · 129 times read
void main()
{
    assert(["test11", "test","test","test1"].distinctArray == ["test11", "test","test1"]);
    assert("ABCDGEefFGG".distinctArray == "ABCDGEefF");
}

T[] distinctArray(T)(in T[] s) {
    import std.algorithm: canFind;
    T[] result;
    foreach (T c; s)
        if (!result.canFind(c))
            result ~= c;
    return result;
}
Login to reply