Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e6899ee

Browse files
author
Khaled Yousef
committed
feat(problem #86): add remove-duplicates-from-sorted-array
1 parent 53f9a9b commit e6899ee

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
internal class Program
2+
{
3+
private static (int, int[]) RemoveDuplicates(int[] nums)
4+
{
5+
switch (nums.Length)
6+
{
7+
case 0:
8+
return (0, []);
9+
case 1:
10+
return (1, nums);
11+
case 2 when nums[0] != nums[1]:
12+
return (2, nums);
13+
}
14+
15+
int k = 1;
16+
int currentDup = nums[0];
17+
for (int i = 1; i < nums.Length; i++)
18+
{
19+
if (nums[i] != currentDup)
20+
{
21+
currentDup = nums[i];
22+
nums[k] = currentDup;
23+
k++;
24+
}
25+
}
26+
27+
return (k, nums);
28+
}
29+
30+
private static void Main(string[] args)
31+
{
32+
// [0,0,1,1,1,2,2,3,3,4]
33+
// [1, 2, 2]
34+
// [1, 2, 3]
35+
int[] nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
36+
var result = RemoveDuplicates(nums);
37+
38+
Console.WriteLine(result.Item1);
39+
Console.WriteLine(string.Join(',', result.Item2));
40+
}
41+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /