Skip to content

Commit 9940d82

Browse files
committed
remove element from array task
1 parent f694b10 commit 9940d82

File tree

1 file changed

+37
-0
lines changed
  • C#/LeetCodeResolves/LeetCodeResolves/Arrays/RemoveElement

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.Arrays.RemoveElement
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var nums = new int[] { 0 , 1 , 0, 1 , 2, 5, 6 };
12+
var result = RemoveElement(nums, 1);
13+
14+
Console.ReadLine();
15+
}
16+
17+
static int RemoveElement(int[] nums, int val)
18+
{
19+
var i = 0;
20+
var n = nums.Length;
21+
while (i < n)
22+
{
23+
if (nums[i] == val)
24+
{
25+
nums[i] = nums[n - 1];
26+
n--;
27+
}
28+
else
29+
{
30+
i++;
31+
}
32+
}
33+
34+
return n;
35+
}
36+
}
37+
}

0 commit comments

Comments
 (0)