Skip to content
Advertisement

Bug in methods that find unmatched integers between two arrays

I am trying to do the following function in the java code below: I am trying to generate the array of integers that do not match between ‘match’ array and ‘original’ array.

To do this I created a subarray class, a class that allows me to get the rest of unmatched elements in the two arrays (match and original), and lastly, I have a class that is called match_sequence that does the job.

My problem is there is a bug in the function that I have no idea why, for example if my input is int[] original = {1,0}; int[] match = {1,0}; I get the residual for both original and match with [0] when it should be [], and if my inputs are {1,0,0},{1,0,0}, there is no such problem but if my inputs are both {1,0,1,0,0}, I got [0, 0] in both elements as unmatched array, when again, the result should be [] because those are perfectly matched arrays.

This is driving me insane can someone please let me know where is the bug?

JavaScript
JavaScript
JavaScript

Advertisement

Answer

The problem is inside match_sequence() method. You slice first element of an array, but you don’t move j index back. To do so add such line at the end of for loop:

JavaScript

Also match_sequence() contains big code duplication. If statements are redudant, you can combine them in one for loop:

JavaScript

Output:

JavaScript

Update:
Picture below shows why you need to move j back. If you don’t do that, then you will skip second element of list.

Why do we need to move j back

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement