Home > Enterprise >  Find Difference between 2 large arrays
Find Difference between 2 large arrays

Time:11-27

I have two byte arrays which can be very large, maybe even 700500 values.

array2 is always larger than array1, and it basically contains the same data as in array1, but with random additions, for example:

int[] array1 = {1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 0, 0, 0};

int[] array2 = {1, 1, 1, 2, 7, 7, 2, 2, 2, 2, 1, 2, 3, 2, 2, 3, 3, 4, 7, 2, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 4, 1, 1, 7, 7, 8, 8, 9, 9, 0, 0};

I need to have an array3, which needs to have the same size as arrays2. It will show the exact indices where the additions are, so for this example it would be:

int[] array3 = {0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0};

(0 = same as in array1, 1 = different than in arrays1)

I want to have the same result as I get in the application "Beyond Compare":

https://i.ibb.co/yX6YCsp/Diff.jpg

but to get the Indexes of the red marks that you see in the picture, on the right pane.

I need to write it in C#.

Thanks very much for all the help on this!

CodePudding user response:

What you're looking for is a diff algorithm, which isn't so easy to do well. I recommend using Google's DiffMatchPatch library instead of writing it yourself, but if you want to go to that route, the Wikipedia article should be a good starting point to learn more about that particular rabbit hole.

CodePudding user response:

You can compare each element between the two arrays. If there is a match add a 0 to array3 and look at the next element in both arrays. If there is no match, add a 1 to array3 and look at the next element in array2. If array1 has no more elements then keep adding 1 until array2 has no more elements.

int[] array1 = {1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 4, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 7, 7, 8, 8, 9, 9, 0, 0, 0};
int[] array2 = {1, 1, 1, 2, 7, 7, 2, 2, 2, 2, 1, 2, 3, 2, 2, 3, 3, 4, 7, 2, 5, 5, 5, 5, 5, 5, 6, 6, 7, 7, 8, 4, 1, 1, 7, 7, 8, 8, 9, 9, 0, 0};

int index1 = 0;
int index2 = 0;

int[] array3 = new int[array2.Length];
while (index2 < array2.Length)
{
    if (index1 >= array1.Length)
    {
        array3[index2] = 1;
        index2  = 1;
    }
    else if (array1[index1] == array2[index2])
    {
        array3[index2] = 0;
        index1  = 1;
        index2  = 1;
    }
    else
    {
        array3[index2] = 1;
        index2  = 1;
    }
}
foreach (int i in array3)
{
    Console.Write(i.ToString()   " ");
}

Output:

0 0 0 0 1 1 0 0 0 0 1 0 0 1 1 0 1 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0

  • Related