Problem 26: IndexOutOfRange Error

I got an runtime error when for problem 26 and I cannot findout why.

public class Solution {
    public int RemoveDuplicates(int[] nums) {
        int tmp, p=1;
        if(nums==null)
            return 0;
        else if(nums.Length<2) 
            return 1;
        else
        {
            tmp = nums[0];

            for (int i=1;i<nums.Length;i++){
                if(nums[i]!=tmp){
                    //If not duplicated numbers
                    nums[p++] = nums[i];
                    tmp = nums[i];
                }//End of if(nums[i]!=tmp)

            }//End of for
            return p;            
        }
    }
}
Comments (0)
No comments yet.