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

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.

Well the first idea that comes to my mind is something like this:

if(array1.Length>array2.Length){
foreach(int k in array1){
foreach(int l in array2){
if(k==l){
k=null;
}
}
}
}else{
foreach(int l in array2){
foreach(int k in array1){ 
if(l==k){
l=null;
}
}
}
} 

Untested, and only works in theorem for when one array is a subset of another, but hopefully its a start 🙂

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

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