coder8888
coder8888

Reputation: 157

Finding difference between two integer arrays

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);
        }
    }
}

Upvotes: 0

Views: 3319

Answers (3)

Adrian Shum
Adrian Shum

Reputation: 40036

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());

Upvotes: 1

Devram Kandhare
Devram Kandhare

Reputation: 781

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);

Upvotes: 0

Titus
Titus

Reputation: 22474

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.

Upvotes: 2

Related Questions