Java 2ms solution


  • 0

    public class Solution {
    public int removeDuplicates(int[] nums) {

        int j = 0;
        int temp = 0;
        
        for(int i=0; i<nums.length; i++){
            if(i==0) {
                temp=nums[0];
                j++; 
            }
            else{
                if(nums[i]!=temp){ 
                    nums[j]=nums[i];
                    j++;
                }
            }
            temp=nums[i];
        }
        nums = Arrays.copyOf(nums, j);
        return nums.length;
    }
    

    }


Log in to reply
 

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