# LeetCode #27 Remove Element Solution & Explanation

Content

## LeetCode Problem

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed.

Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are k elements after removing the duplicates, then the first k elements of nums should hold the final result. It does not matter what you leave beyond the first k elements.

Return k after placing the final result in the first k slots of nums.

Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.

## Solution

When I first saw this problem, I thought that was so easy, just a piece of cake !

### C# Solution

First try

``````public class Solution {
public int RemoveElement(int[] nums, int val) {

//ANS1
/*
int []expectedNums  = nums.Where(s => s != val).ToArray();
int l = expectedNums.Length;

for (int i = 0; i <l ; i++) {
nums[i] = expectedNums[i];
}

return l;
*/
//ANS2

int[]x = new int[nums.Length];

int cnt = 0;
foreach(int i in nums)
{
if(i!=val)
{
x[cnt] = i;
cnt+=1;
}
}

for (int i = 0; i <cnt ; i++) {
nums[i] = x[i];
}
return cnt;

}
}``````

Both of the solution had succussed, but when I look at the problem carefully, I found out one thing…

【Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.】

Oh my god, both of the solution had used extra space, let’s try another way.

Solution1

``````public class Solution {
public int RemoveElement(int[] nums, int val) {

int j = 0;
for(int i=0;i<nums.Length;i++)
{
if(nums[i]!=val)
{
nums[j++] = nums[i];
}
}
return j;
}
}``````

Runtime : 134ms、128ms、155ms

Solution2

``````public class Solution {
public int RemoveElement(int[] nums, int val) {

int j = 0;
foreach(int i in nums)
{
if(i!=val)
{
nums[j++] = i;
}
}

return j;
}
}``````

We can also use foreach to replace classic for iteration

### Java Solution

Solution1

``````class Solution {
public int removeElement(int[] nums, int val) {

int j = 0;
for(int i=0;i<nums.length;i++)
{
if(nums[i]!=val)
{
nums[j++] = nums[i];
}
}
return j;
}
}``````

Runtime : 0ms、0ms、0ms

### Python3 Solution

Solution1

``````class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
j = 0
for i in range(0,len(nums)):
if(nums[i]!=val):
nums[j] = nums[i]
j+=1
return j``````

Runtime : 44ms、36ms、47ms

### JavaScript Solution

Solution1

``````/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
var j = 0;
for(var i=0;i<nums.length;i++)
{
if(nums[i]!=val)
{
nums[j++] = nums[i];
}
}
return j;
};``````

Runtime : 72ms、55ms、80ms

## Conclusion

🧡If my solution helps, that is my honor!

The problem link : Remove Element – LeetCode

Latest Post

#### LeetCode #374 Guess Number Higher or Lower Solution & Explanation

Solve the Guess Game by using binary search to efficiently find a picked number between…

#### LeetCode #349 Intersection of Two Arrays Solution & Explanation

Given two integer arrays, find and return their intersection as an array.

#### LeetCode #506 Relative Ranks Solution & Explanation

Given an array representing athletes’ scores, return their respective ranks.

#### LeetCode #292 Nim Game Solution & Explanation

Easily tackle the Nim game using mathematics.

#### LeetCode #881 Boats to Save People Solution & Explanation

Find the minimum number of boats needed to carry all people, considering each boat can…

#### LeetCode #2441 Largest Positive Integer That Exists With Its Negative Solution & Explanation

Find the largest positive integer in the array that has its negation present in the…