Currently, I am testing every integer element against each other to find which ones match. The arrays do not contain duplicates within their own set. Also, the arrays are not always equal lengths. Are there any tricks to speed this up? I am doing this thousands of times, so it's starting to become a bottle neck in my program, which is in C#.
+5
A:
Use a HashSet
var set = new HashSet<int>(firstArray);
set.IntersectWith(secondArray);
The set now contains only the values that exist in both arrays.
Josh Einstein
2010-04-10 18:51:19
I think you want .Intersect rather than .Union
Hightechrider
2010-04-10 19:01:48
Ahh brain fart! Thanks. I edited it.
Josh Einstein
2010-04-10 19:08:14
Just tried the HashSet with IntersectWith and it's twice as slow compared to iterating over all elements.
John Sheares
2010-04-10 21:13:47
+6
A:
You could use LINQ:
var query = firstArray.Intersect(secondArray);
Or if the arrays are already sorted you could iterate over the two arrays yourself:
int[] a = { 1, 3, 5 };
int[] b = { 2, 3, 4, 5 };
List<int> result = new List<int>();
int ia = 0;
int ib = 0;
while (ia < a.Length && ib < b.Length)
{
if (a[ia] == b[ib])
{
result.Add(a[ia]);
ib++;
ia++;
}
else if (a[ia] < b[ib])
{
ia++;
}
else
{
ib++;
}
}
Mark Byers
2010-04-10 19:00:35
John already stated that the arrays are ordered in comments above.
Robert Jeppesen
2010-04-10 19:16:20
A:
If such a comparison is a bottleneck in your program, you are perhaps using an inappropriate data structure. The simplest way might be to keep your data sorted. Then for finding out the common entries, you would need to traverse both arrays only once. Another option would be to keep the data in a HashSet.
Vlad
2010-04-10 19:01:57