Q:

Converting Primitive int array to list

I am trying to solve the following problem. There are  two arrays A of size n and B of size n+1. A and B have all elements same. B has one extra element. Find the element.
My logic is to convert the array to list and check if each element in B is present in A.
But when I am using the primitive arrays my logic is not working. If I am using 
Integer [] a ={1,4,2,3,6,5};
Integer [] b = {2,4,1,3,5,6,7};

My code is working fine.
public static void main(String [] args)
{
    int [] a ={1,4,2,3,6,5};
    int [] b = {2,4,1,3,5,6,7};     
    List<Integer> l1 = new ArrayList(Arrays.asList(a));
    List<Integer> l2 = new ArrayList(Arrays.asList(b));
    for(Integer i :l2)
    {
        if(!l1.contains(i))
        {
            System.out.println(i);
        }           
    }
}

And also My logic is O(n+1). Is there any better algo.
Thanks

A:

The reason it is not working for primitive arrays, is that Arrays.asList when given an int[ ] returns a List<Integer[ ]> rather than a List<Integer>.
Guava has an answer to this in the Ints class. Is has an asList method that will take an int[ ] and return a List<Integer>
Update
int[] a = ...;
int[] b = ...;
List<Integer> aList = Ints.asList(a);
List<Integer> bList = Ints.asList(b);

The above will allow your code to work properly for int[ ] as it works for Integer[ ].
Check out Ints API

A:

Calculate the sum of each array.  Sum(B) - Sum(A) will give you the extra element in B.

A:

The contains method of an ArrayList is in fact not so efficient. This allone has a runtime of O(n), so your algorithm has runtime O(n^2).
I suggest to put one array in a HashSet (with a contains() runtime of O(1)). You can leave the other array as it is and iterate through the array directly. Then your runtime is O(n).
Update:
From the HashSet API doc:

This class offers constant time performance for the basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets.

I think the default Integer hash function fullfills the requirements.

