D idom for removing array elements

Stefan Koch via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 26 09:46:02 PST 2017


On Thursday, 26 January 2017 at 11:44:27 UTC, Nicholas Wilson 
wrote:
> On Thursday, 26 January 2017 at 08:22:09 UTC, albert-j wrote:
>> What is the D idiom for removing array elements that are 
>> present in another array?
>>
>> Is this the right/fastest way?
>>
>> int[] a = [1, 2, 3, 4, 5, 6, 7, 4];
>> int[] b = [3, 4, 6];
>> auto c = a.remove!(x => b.canFind(x));
>> assert(c == [1, 2, 5, 7]);
>
> filter.
>
> auto c = a.filter!(x => !b.canFind(x)).array;

He wanted to remove elements.
This will allocate a freaking new array.
And do N function calls to build it.
This is WORSE!



More information about the Digitalmars-d-learn mailing list