Skip to content

Commit 1b06d40

Browse files
committed
[Easy] Title: 26. Remove Duplicates from Sorted Array - LeetCode
1 parent 923764e commit 1b06d40

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# [Easy] 26. Remove Duplicates from Sorted Array
2+
3+
[문제 링크](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)
4+
5+
### 문제 설명
6+
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. Then return the number of unique elements in nums.
7+
8+
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
9+
10+
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
11+
Return k.
12+
Custom Judge:
13+
14+
The judge will test your solution with the following code:
15+
```
16+
int[] nums = [...]; // Input array
17+
int[] expectedNums = [...]; // The expected answer with correct length
18+
19+
int k = removeDuplicates(nums); // Calls your implementation
20+
21+
assert k == expectedNums.length;
22+
for (int i = 0; i < k; i++) {
23+
assert nums[i] == expectedNums[i];
24+
}
25+
```
26+
If all assertions pass, then your solution will be accepted.
27+
28+
### 입출력
29+
Example 1:
30+
```
31+
Input: nums = [1,1,2]
32+
Output: 2, nums = [1,2,_]
33+
```
34+
Explanation: Your function should return k = 2, with the first two elements of nums being 1 and 2 respectively.
35+
It does not matter what you leave beyond the returned k (hence they are underscores).
36+
37+
Example 2:
38+
```
39+
Input: nums = [0,0,1,1,1,2,2,3,3,4]
40+
Output: 5, nums = [0,1,2,3,4,_,_,_,_,_]
41+
```
42+
Explanation: Your function should return k = 5, with the first five elements of nums being 0, 1, 2, 3, and 4 respectively.
43+
It does not matter what you leave beyond the returned k (hence they are underscores).
44+
45+
### 문제설명
46+
Constraints:
47+
- 1 <= nums.length <= 3 * 104
48+
- -100 <= nums[i] <= 100
49+
- nums is sorted in non-decreasing order.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public int removeDuplicates(int[] nums) {
3+
if(nums.length == 0) {
4+
return 0;
5+
}
6+
7+
int p = 0;
8+
int start = 0;
9+
while(p <= nums.length - 1) {
10+
if(nums[start] != nums[p]) {
11+
start++;
12+
nums[start] = nums[p];
13+
}
14+
p++;
15+
}
16+
17+
return start + 1;
18+
}
19+
}

0 commit comments

Comments
 (0)