Problem:
I have two arrays that can possibly be different lengths. I need to iterate through both arrays and find similarities, additions, and deletions.
Since your arrays are sorted, you should be able to just go through the arrays simultaneously, and in one pass and determine if each element is in the other array. (Similar to the merge step in merge sort.) You can see a sample of that below:
string[] oldVersion = { "test1", "test2", "test3" };
string[] newVersion = { "test1", "test2", "test4", "test5" };
int oldIndex = 0, newIndex = 0;
while ((oldIndex < oldVersion.Length) && (newIndex < newVersion.Length)) {
int comparison = oldVersion[oldIndex].CompareTo(newVersion[newIndex]);
if (comparison < 0)
Console.WriteLine("[Removed]\t" + oldVersion[oldIndex++]);
else if (comparison > 0)
Console.WriteLine("[Added]\t\t" + newVersion[newIndex++]);
else {
Console.WriteLine("[Same]\t\t" + oldVersion[oldIndex++]);
newIndex++;
}
}
while (oldIndex < oldVersion.Length)
Console.WriteLine("[Removed]\t" + oldVersion[oldIndex++]);
while (newIndex < newVersion.Length)
Console.WriteLine("[Added]\t\t" + newVersion[newIndex++]);
Alternatively you'd need to go through one array, and for each element in this array, do a single pass of the other array looking for a match.
Edit: JP has a good suggestion on how to do this using the framework. Although, assuming the arrays are sorted, the benefit of my approach is that you only have to do one pass to find all the results. You would not have to do three passes.