Remove Duplicates from Sorted Array

Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same.

Example 1:
Input: nums = [1,1,2]
Output: 2, nums = [1,2,_]

Example 2:
Input: nums = [0,0,1,1,1,2,2,3,3,4]
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]


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

0 Comments

Leave a reply

* Your email/mobile address will not be published. required fields are marked.

* Email and mobile number will be verified before publishing the comments.

Name *
Email *
Mobile