Elegant way to test if members of array A are present in array B?

Paul Backus snarwin at gmail.com
Tue Jun 11 17:34:00 UTC 2019


On Tuesday, 11 June 2019 at 17:12:17 UTC, Robert M. Münch wrote:
> Is there a simple and elegant way to do this? Or is just using 
> a foreach(...) with canFind() the best way?

It's a space/time tradeoff. foreach with canFind is O(n^2) time 
and O(1) space. If you use an associative array or a set, it's 
O(n) time and O(n) space. If you sort the arrays and use 
std.algorithm.setops.setDifference it's O(n*log(n)) time and 
either O(1) or O(n) space, depending on whether you use an 
in-place sorting algorithm.


More information about the Digitalmars-d-learn mailing list