Home LeetCode. 26. Remove Duplicates from Sorted Array
Post
Cancel

LeetCode. 26. Remove Duplicates from Sorted Array

image

[Link] https://leetcode.com/problems/remove-duplicates-from-sorted-array/


1
2
3
4
5
6
7
8
9
10
class Solution {
    public int removeDuplicates(int[] nums) {
      int newIdx = 0, curIdx = 1;
      while(curIdx < nums.length) {
        if(nums[curIdx] != nums[newIdx]) nums[++newIdx] = nums[curIdx];
        curIdx++;
      }
      return newIdx + 1;
    }
}
This post is licensed under CC BY 4.0 by the author.