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?

import java.util.Arrays;
 
public class subarray
{
    // Generic method to get subarray of a non-primitive array
    // between specified indices
    public static<T> int[] subArray(int[] arr, int beg, int end) {
        return Arrays.copyOfRange(arr, beg, end+1);
    }
}
import java.util.Arrays;

public class results {     
      private int[] array1; //array2
      private int[] array2; //array1

      public results (int[] result1, int[] result2)
      {
         array1 = result1;
         array2 = result2;
         //return results(array1,array2);
      }
      public int[] getArray1() { return array1; }
      public int[] getArray2() { return array2; }
}
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class match_sequence {
    
    public static results match_sequence(int[] original_rest,int[] match)  {

        if(match.length <= original_rest.length) {
            for (int j = 0; j < match.length; j++) {
                //set a initial starting point plus i if we are not starting from 0
                if (original_rest[j]==match[j]) {
                    match = subarray.subArray(match,j+1,match.length-1);
                    original_rest = subarray.subArray(original_rest,j+1,original_rest.length-1);
                } else if (original_rest[j]!=match[j]) {
                    original_rest = subarray.subArray(original_rest,j,original_rest.length-1);
                    match = subarray.subArray(match,j,match.length-1);
                    if(original_rest[0]!=match[0]){
                        System.out.println("This is not an interweave of two sub elements!");
                    }
                    break;
                }
            }
        }
        if(match.length > original_rest.length) {
            for (int j = 0; j < original_rest.length; j++) {
                //set a initial starting point plus i if we are not starting from 0
                if (original_rest[j]==match[j]) {
                    match = subarray.subArray(match,j+1,match.length-1);
                    original_rest = subarray.subArray(original_rest,j+1,original_rest.length-1);
                } else if (original_rest[j]!=match[j]) {
                    original_rest = subarray.subArray(original_rest,j,original_rest.length-1);
                    match = subarray.subArray(match,j,match.length-1);
                    if(original_rest[0]!=match[0]) {
                        System.out.println("This is not an interweave of two sub elements!");
                    }
                    break;
                }
            }
        }

        return new results(match,original_rest);
    }
    //here is the problem:  
    public static void main(String[] args)
    {
        int[] original = {1,0};
        int[] match = {1,0};
        //bug1: there is mismatch over here and should be resolved through debugging
        int[] rest_of_match = match_sequence(original,match).getArray1();
        System.out.println("Final match"+Arrays.toString(rest_of_match));
        int[] original_rest = match_sequence(original,match).getArray2();
        System.out.println("Final original"+Arrays.toString(original_rest));
    }
}

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:

j--;

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

public static results match_sequence(int[] original_rest, int[] match) {
    for(int j = 0; j < Math.min(match.length, original_rest.length); j++) {
        //set a initial starting point plus i if we are not starting from 0
        if(original_rest[j] == match[j]) {
            match = subarray.subArray(match, j + 1, match.length - 1);
            original_rest = subarray.subArray(original_rest, j + 1, original_rest.length - 1);
        } else if(original_rest[j] != match[j]) {
            original_rest = subarray.subArray(original_rest, j, original_rest.length - 1);

            match = subarray.subArray(match, j, match.length - 1);
            if(original_rest[0] != match[0]) {
                System.out.println("This is not an interweave of two sub elements!");
            }
            break;
        }
        j--;
    }

    return new results(match, original_rest);
}

Output:

Final match[]
Final original[]

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