Elegant way to test if members of array A are present in array B?
Murilo
murilomiranda92 at hotmail.com
Wed Jun 12 00:40:06 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?
I made it this way, I consider it elegant. I don't know if others
will like it.
Here it is:
import std.stdio : writeln;
import std.algorithm.searching;
import std.algorithm.sorting;
void main()
{
int[] a = [3, 9, 1, 4, 7, 6, 5, 8, 2];
int[] b = [5, 4, 6];
//first we sort both of them
sort(a);
sort(b);
//now we check them using slices
writeln(b == a[a.countUntil(b[0]) .. a.countUntil(b[$ - 1]) +
1]);
}
It should output `true`
More information about the Digitalmars-d-learn
mailing list