Fast Java Solution


  • 0
    S
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int n = nums.length;
            if ((n == 0) || (n == 1)) return n;
            int lastInt = nums[0];
            int lastIndex = 1;
            // Run through all items in the list
            for (int i = 1; i < n; i++) {
                if (nums[i] != lastInt) {
                    nums[lastIndex] = nums[i];
                    lastInt = nums[i];
                    lastIndex++;
                }
            }
            // Return length
            return lastIndex;
        }
    }

Log in to reply
 

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