Skip to content

Commit beaa36f

Browse files
committed
feat(remove-duplicates-from-sorted-array): solution for problem 0026
1 parent df882d5 commit beaa36f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode id=26 lang=cpp
3+
*
4+
* [26] Remove Duplicates from Sorted Array
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public:
10+
int removeDuplicates(vector<int>& nums) {
11+
12+
if (nums.size() == 0)
13+
return 0;
14+
15+
int i = 0; // index of the current element
16+
int j = 0; // index of the next element
17+
while (j < nums.size()) // while there are still elements to compare
18+
{
19+
if (nums[i] != nums[j]) // if the current element is not the same as the next element
20+
{
21+
i++; // increment the index of the current element
22+
nums[i] = nums[j]; // assign the next element to the current element
23+
}
24+
j++; // increment the index of the next element
25+
}
26+
return i + 1;
27+
}
28+
};
29+
// @lc code=end
30+

0 commit comments

Comments
 (0)