Home Leetcode 26. Remove Duplicates from Sorted Array
Post
Cancel

Leetcode 26. Remove Duplicates from Sorted Array

Leet code problem 26
Difficulty: Easy

Solution

  • Use 2 variables curIndex & uniqueCount
  • Iterate the whole nums array using curIndex. If there are any different between current and previous -> add it to the uniqueCount index

Implementation
Code from github

1
2
3
4
5
6
7
8
9
10
11
12
public class L26_RemoveDuplicatesFromSortedArray {
    public int removeDuplicates(int[] nums) {
        int uniqueCount = 1;
        for (int curIndex = 1; curIndex < nums.length; curIndex++) {
            if (nums[curIndex] != nums[curIndex - 1]) {
                nums[uniqueCount] = nums[curIndex];
                uniqueCount++;
            }
        }
        return uniqueCount;
    }
}
This post is licensed under CC BY 4.0 by the author.