On Wed, Jan 16, 2019 at 03:57:49PM +0000, bauss via Digitalmars-d-learn wrote:
> Is there a way to achieve the following:
[...]
> enum Foo : string
> {
>     a = "aa",
>     b = "bb",
>     c = "cc"
> }
> 
> void main()
> {
>     auto a = [Foo.a, Foo.b, Foo.a, Foo.b, Foo.c];
> 
>     auto b = a.uniq;
> 
>     writeln(b);
>     // Expected output: [a, b, c]
>     // Outputs: [a, b, a, b, c]
> }

.uniq only works on adjacent identical elements.  You should sort your
array first.

If you need to preserve the original order but eliminate duplicates,
then you could use an AA to keep track of what has been seen. E.g.:

        bool[string] seen;
        auto b = a.filter!((e) {
                        if (e in seen) return false;
                        seen[e] = true;
                        return true;
                });


T

-- 
People walk. Computers run.

Reply via email to