I'm having a problem with my work that hopefully reduces to the following: I have two List<int>
s, and I want to see if any of the int
s in ListA
are equal to any int
in ListB
. (They can be arrays if that makes life easier, but I think List<>
has some built-in magic that might help.) And I'm sure this is a LINQ-friendly problem, but I'm working in 2.0 here.
My solution so far has been to foreach
through ListA, and then foreach
through ListB,
foreach (int a in ListA)
{
foreach (int b in ListB)
{
if (a == b)
{
return true;
}
}
}
which was actually pretty slick when they were each three items long, but now they're 200 long and they frequently don't match, so we get the worst-case of N^2 comparisons. Even 40,000 comparisons go by pretty fast, but I think I might be missing something, since N^2 seems pretty naive for this particular problem.
Thanks!