Loading collection data...
Collections are a way for you to organize kata so that you can create your own training routines. Every collection you create is public and automatically sharable with other warriors. After you have added a few kata to a collection you and others can train on the kata contained within the collection.
Get started now by creating a new collection.
This is perfect ;(
return a.Any(b.Contains);
if array is null it doesn't work
Yes, definitely simpler. Not sure about more performant...maybe.
Perhaps then
a.Intersect(b).Any()
- even simpler and more performant@amandakleeen Intersect enumerates the source; in
a.Intersect(b)
all ofb
is added to a set in a loop, then all ofa
is removed from that set in another loop. If the removal succeeds the item froma
is returned. The removal loop is ayield
ing operation, meaning that as soon as an intersecting element is encountered (set.Remove
returns true) it is returned etc. so it would be slightly more efficient to ask forAny()
rather thanCount()
. This is becauseAny
will quit enumerating the intersection as soon as it discovers there to be one item.Count
will count all of them (fully enumerate), which is unnecessary if all we seek is to know if there is at least 1. Side note (which you've done) - there are differences in memory too; becauseb
is turned into a hashset against whicha
is compared, on a system with more CPU than memory resource it may be better to makeb
the smaller of the two arrays@phil-port under the hood, this approach could require a lot of comparisons, upto
a.Length * b.Length
.c# - I tried solvning this using Distinct(), but i get this error, "src/Solution.cs(8,28): error CS1061: 'IEnumerable' does not contain a definition for 'Distinct' and no accessible extension method 'Distinct' accepting a first argument of type 'IEnumerable' could be found (are you missing a using directive or an assembly reference?)". I get en issue trying to use ToArray() too. However it works when i do it in VS, does anybody else get this issue?