Software engineer

LC-Remove Duplicates from Sorted Array

LC-Remove Duplicates from Sorted Array

Remove Duplicates from Sorted Array

Question

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. Then return the number of unique elements in nums.

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[] newAr=new int[nums.length];
        int j=0;
        for(int i=1;i<nums.length;i++){
            if(nums[i]!=nums[j]){
                nums[j+1]=nums[i];
                j++;
            }
        }return j+1;
    }
}
comments powered by Disqus