What is wrong with my java code


#1
public class Solution {
public int removeDuplicates(ArrayList<Integer> a) {
    int k = 0;
    for(int i=1;i<a.size();i++)
    {
        if(a.get(i) != a.get(k))
        {
            k++;
            a.set(k,a.get(i));
        }
    }
    return k+1;
}

}


#2

here you are comparing Integers with !=.
This will work for small Integers that are internalized by the JVM but in general is wrong.
you should either cast the vales to int (primitive) or use !v1.equals(v2) instead of !=.


#3

if (!a.get(i).equals(a.get(k)))


#4

You have to remove extra integer from index k+1 to a.size()-1.

a.subList(k+1,a.size()).clear();
// remove sublist from index k+1 to index a.size()-1.