D idom for removing array elements

albert-j via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jan 26 15:10:02 PST 2017


On Thursday, 26 January 2017 at 13:21:38 UTC, Dukc wrote:

> import std.stdio, std.algorithm, std.range, std.array;
> int[] a = [1, 2, 3, 4, 5, 6, 7, 4];
> int[] b = [3, 4, 6];
> auto sortedB = sort(b.dup);
> auto c = a
> .   filter!(i => !sortedB.contains(i))
> .   array
> ;
> assert(c == [1, 2, 5, 7]);
>
> If arrays get large, this should be more efficient since it 
> performs O(n * n.log) instead of O(n * n).

It does look much faster than my solution. Will it also work 
correctly and fast for arrays of custom objects? How should 
opCmp() be defined if objects don't have a meaningful ordering? 
The order of elements in the original array does not matter.


More information about the Digitalmars-d-learn mailing list