26. Remove Duplicates from Sorted Array
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>_26._Remove_Duplicates_from_Sorted_Array</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
2
26. Remove Duplicates from Sorted Array/Program.cs
Normal file
2
26. Remove Duplicates from Sorted Array/Program.cs
Normal file
@@ -0,0 +1,2 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
16
26. Remove Duplicates from Sorted Array/Solution.cs
Normal file
16
26. Remove Duplicates from Sorted Array/Solution.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
public class Solution {
|
||||
public int RemoveDuplicates(int[] nums) {
|
||||
var k = nums.Length;
|
||||
var putIndex = 0;
|
||||
for(var i = 0; i < nums.Length; i++)
|
||||
{
|
||||
while(i < nums.Length - 1 && nums[i+1] == nums[i])
|
||||
{
|
||||
i++;
|
||||
k--;
|
||||
}
|
||||
nums[putIndex++] = nums[i];
|
||||
}
|
||||
return k;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user