Skip to content

Commit 686f44c

Browse files
committed
https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/
1 parent 1d9dd5b commit 686f44c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

LeetCodeSolutions/ArrayAndString/MergeSortedArray.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using NUnit.Framework;
21

32
namespace LeetCodeSolutions.ArrayAndString;
43

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+

2+
3+
namespace LeetCodeSolutions.ArrayAndString
4+
{
5+
class Remove_Duplicates_from_Sorted_Array_II
6+
{
7+
public int RemoveDuplicates(int[] nums)
8+
{
9+
if (nums == null || nums.Length <= 2) return nums?.Length ?? 0;
10+
11+
int k = 2;
12+
for (int i = 2; i < nums.Length; i++)
13+
{
14+
if (nums[i] != nums[k - 2])
15+
{
16+
nums[k++] = nums[i];
17+
}
18+
}
19+
return k;
20+
}
21+
22+
[Test(Description = "https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/")]
23+
[Category("Medium")]
24+
[Category("LeetCode")]
25+
[Category("Remove Duplicates from Sorted Array II")]
26+
[Category("Array/String")]
27+
[Category("TopInterview")]
28+
[TestCaseSource(nameof(Input))]
29+
public void Test1((int Output, int[] Input) item)
30+
{
31+
var response = RemoveDuplicates(item.Input);
32+
Assert.That(response, Is.EqualTo(item.Output));
33+
}
34+
35+
public static IEnumerable<(int Output, int[] Input)> Input =>
36+
new List<(int Output, int[] Input)>()
37+
{
38+
(5, [1,1,1, 1,2,2,3]),
39+
(7, [0,0,1,1,1,1,2,3,3]),
40+
(5, [1,1,1,2,2,3]),
41+
};
42+
}
43+
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ The Top Interview 150 collection on Leetcode is a curated set of 150 interview q
4747
| 1 | Merge Sorted Array | Easy ||
4848
| 2 | Remove Element | Easy ||
4949
| 3 | Remove Duplicates from Sorted Array | Easy ||
50-
| 4 | Remove Duplicates from Sorted Array II | Medium | |
50+
| 4 | Remove Duplicates from Sorted Array II | Medium | |
5151
| 5 | Majority Element | Easy ||
5252
| 6 | Rotate Array | Medium | |
5353
| 7 | Best Time to Buy and Sell Stock | Easy | |

0 commit comments

Comments
 (0)