0

I'm writing a function which returns the difference between two integer arrays. I'm assuming that that all elements in input arrays are unique and also the input arrays are not sorted. For Example:

Input:
arr1 = [1,2,3,5,4]
arr2 = [1,2,3]

Expected Output: [4,5]

My Output: [1,2,3,4,5] (when first array is larger than second)

When I make the second array larger than first, I get ArrayIndexOutOfBoundsException.

public class Test{

     public static void main(String args[])
    {

        Scanner sc = new Scanner(System.in);
        System.out.println("Enter length of first array");
        int ml = sc.nextInt();
        System.out.println("Enter length of second array");
        int nl = sc.nextInt();
        int m[] = new int[ml];
        int n[] = new int[nl];
        System.out.println("Enter elements of first array");
        for(int i=0;i<ml;i++)
        {
            m[i] = sc.nextInt();
        }

        System.out.println("Enter elements of second array");
        for(int j=0;j<nl;j++)
        {
            m[j] = sc.nextInt();
        }
        ArrayList<Integer> arr1 = new ArrayList<Integer>();
        for(int i: m){ arr1.add(i);}
        ArrayList<Integer> arr2 = new ArrayList<Integer>();
        for(int j: n){ arr2.add(j);}
        if(ml>nl)
        {
            arr1.removeAll(arr2);
            System.out.println(arr1);
        }
        else
        {
            arr2.removeAll(arr1);
            System.out.println(arr2);
        }
    }
}
2
  • 1
    I strongly recommend you step through this with a debugger. You'll be able to see exactly what's going on. Commented Aug 1, 2017 at 3:16
  • 1
    Use try catch blocks for appropriate exception handling. Commented Aug 1, 2017 at 3:25

3 Answers 3

2

In the second iteration you should use n[j] = ... instead of m[j] = ... You should use more descriptive variable names to prevent thing kind of thing from happening.

Sign up to request clarification or add additional context in comments.

Comments

1

If it is not a homework that let you practice on array/iterations, you may consider using Set for much simpler logic:

// pseudo-code
Set<Integer> set1 = new HashSet<>(array1);
Set<Integer> set2 = new HashSet<>(array2);

// Using Guava
Set<Integer> diff = Sets.symmetricDifference(set1, set2);

// Java only
Set<Integer> diff1 = new HashSet<>(set1);
diff1.removeAll(set2);  // diff1 contains entries in array1 but not 2
Set<Integer> diff2 = new HashSet<>(set2);
diff2.removeAll(set1);  // diff2 contains entries in array2 but not 1

Set<Integer> diff = new HashSet<>(set1);
diff.addAll(set2);


// Java only, using stream
return Stream.concat(set1.stream(), set2.stream())
             .filter(i -> ! (set1.contains(i) && set2.contains(i)))
             .collect(Collectors.toSet());

Comments

0

You can check if element exist in second one if not can add to output array as below:

    int[] array1 = new int[] { 1, 2, 3, 4, 5 };
    int[] array2 = new int[] { 1, 2, 3 };
    List<Integer> output = new ArrayList<>();
    for (int i = 0; i < array1.length; i++) {
        boolean flag = false;
        for (int j = 0; j < array2.length; j++) {
            if (array1[i] == array2[j]) {
                flag = true;
                break;
            }
        }
        if (!flag) {
            output.add(array1[i]);
        }
    }

    System.out.println(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.