2ms Java Solution, O(n)


  • 2
    G
    public class Solution {
        public int removeDuplicates(int[] nums) {
            if(nums.length == 0 || nums == null) { return 0; }
            if(nums.length == 1) { return 1; }
            
            int unique = 0; //Unique always point to non-duplicate integer
            for(int j = 1; j < nums.length; j++) {
                if(nums[j] != nums[unique]) {
                    nums[++unique] = nums[j]; //assign the first integer on the right side of unique as jth element of array
                }
            }
            return unique + 1;         
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.