LeetCode OJ 26 Remove Duplicates from Sorted Array

Question

[LeetCode 26] Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.

Submission

Java Submission
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Solution {
public int removeDuplicates(int[] nums) {
if (nums.length == 0) return 0;

int length = 1;

for (int i = 0; i < nums.length; i++) {
for (int k = i; k < nums.length; k++) {
i = k;
if (nums[k] != nums[length-1]) {
nums[length++] = nums[k];
break;
}
}
}

return length;
}
}