D idom for removing array elements

Jordan Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 26 11:26:53 PST 2017


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]);

If you don't care about array a being mutated, then I think what 
you have is best (although I would suggest that if you don't care 
about a being mutated, just reassign the results back to a again).

Otherwise, I think you need to allocate a new array, so the other 
answers using filter are good.


More information about the Digitalmars-d-learn mailing list