Easy Java Solution to minimize array writes


  • 0
    public class Solution {
        public int removeDuplicates(int[] nums) {
            int shiftCount = 0;
            for(int i=0; i<nums.length-1; i++) {
                if(nums[i]==nums[i+1]) {
                    shiftCount++;
                } else if (shiftCount!=0) {
                    nums[i+1-shiftCount] = nums[i+1];
                }
            }
            return nums.length-shiftCount;
        }
    }

Log in to reply
 

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