Leet code problem 26
Difficulty: Easy —
Solution
- Use 2 variables
curIndex
&uniqueCount
- Iterate the whole
nums
array usingcurIndex
. If there are any different between current and previous -> add it to theuniqueCount
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;
}
}