leetcode remove duplicate from array

#670
Raw
Author
socdev
Created
Feb. 10, 2023, 1:04 p.m.
Expires
Never
Size
557 bytes
Hits
155
Syntax
Java
Private
No
class Solution {
    //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.
    public int removeDuplicates(int[] nums) {
        int newIdx = 1;
        for (int i = newIdx; i < nums.length; i++) {
            if (nums[newIdx - 1] != nums[i]){
                nums[newIdx] = nums[i];
                newIdx++;
            }
        }
        return newIdx;
        
    }
}