Java Solution with Explanation


  • 14
    M
    public class Solution {
        public int removeDuplicates(int[] nums) {
        
            int i = 1; //iterator thru array
            int j = 0; //current index
            for (; i<nums.length; i++) { 
                if (nums[i] != nums[j]) { //new number
                    j++; //move current index
                    nums[j] = nums[i]; //fill current index with new number
                } 
            }
        return j+1;
       }
    }

Log in to reply
 

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