# Remove Element

• Hi, I have a problem with running solution.

I wrote the next solution: http://take.ms/vnqSp (it may not be optimal but it doesn't matter in this case). As you can see, the result is an array with 2 elements: {2, 2}.

But running code on your mashine show me the next output: http://take.ms/IQEhg ({3, 2}) I use neither global variables, nor C/C++. What could be the problem here?

• @ivan70 The reason is you're assigning an array to nums inside the function, and the caller has no idea that nums had changed. This is similar to the following:

``````public void foo(int x) {
x = 3;  // change the copy of x to 3, which had no effect on the original x.
}

int x = 4;
foo(x);
System.Console.WriteLine(x);   // still prints 4.
``````

Therefore, the only way to change nums is to do something like `nums[0] = 4;`, which will change the values in nums array. This is how `in-place` modification works.

• @1337c0d3r Oh, you're right, how could I miss that? :) Thank you.

• `````` for(int i=0,j=0;i<nums.length&&j<nums.length;)
{
if(nums[j]!=val)
{
if(j-i>0)
{
f=1;
nums[i]=nums[j];
nums[j]=-11111;

}

i++;
j++;
continue;

}
else{

j++;
}

}

for(int i=0;i<nums.length;i++){
if((nums[i]==val||nums[i]==-11111))
return i;

}
``````

• Hi,
could anyone explain why the following wont work with python?
while val in list:
list.remove(val)

thanks!

• @michahell I'm not sure what you mean by "wont work". I submitted your solution, and LeetCode accepted it as valid. It ranked among one of the slower solutions. Here are some concerns about your solution:

1. Python's built-in remove function might not be in-place. It might generate a new list which violates one of the problem's constraints. However, I'm betting it really is in-place. The built-in functions are coded to be fast and efficient.

2. The solution is slow. 'in' is O(n) and 'remove' is O(n), making your solution O(n^2). You should aim for an O(n) solution, though this wasn't explicitly specified as a constraint.

• Hi,how can you make sure that nums[n-1] doesn't equal to val in approach 2?

• In Approach #2 while(i < n) should be changed to while(i <= n), or it will be wrong in some test cases.

• sorry, while(i<n) is right!

• what if the rare number is in the middle, still need to travel to the whole n

• ``````int removeElement(vector<int>& nums, int val) {

auto pend = std::remove_if(nums.begin(), nums.end(), [val](const int elem) {
return (elem == val);
});

return pend - nums.begin();
}``````

• avoid the useless write.

``````public int removeElement(int[] nums, int val) {
int l = 0, r = nums.length-1;
while(l < r) {
while(l < nums.length && nums[l] != val) l++;
while(r >= 0 && nums[r] == val) r--;
if(l < r) {
nums[l] = nums[r];
nums[r] = val;
l++;
r--;
} else break;
}
if(l == r && nums[l] != val) l++;
return l;
}
``````

• /**

• @param {number[]} nums
• @param {number} val
• @return {number}
*/
var removeElement = function(nums, val) {
return nums.filter(function(v, i){
return v !== val
})
};

• class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int insert_index=0;
int cnt=0;
for(int i=0;i<nums.size();i++)
{
if(nums[i]!=val)nums[insert_index++] = nums[i];
else cnt++;
}
return nums.size()-cnt;
}
};

• My solution is similar to solution 2 but adding an step to ensure that nums[n-1], the number swapped with the disposed one, must be a valid number.
class Solution {
public:
int removeElement(vector<int>& nums, int val) {
if (nums.empty()) return 0;
int i = 0, j= nums.size();
while (i<nums.size()) {
if (nums[i] == val) {
while (j>i) {
--j;
if (nums[j] != val) {
//switch nums[i] and nums[j]
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
if (i >= j) break;
}
++i;
}
return i;
}
};

• Can someone tell me why it's not accepting my answer?

public int removeElement(int[] nums, int val) {
int countOfVal = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == val) countOfVal++;
}
return nums.length - countOfVal;
}

• @kvelury
You need to modify the array itself. Instead of just return the count.

• Here is my JavaScript solution:

let removeElement = function(nums, val) {
let len = nums.length;

if (len === 0) {
return len;
}

let i = len;
while (i--) {
if (nums[i] === val) {
nums.splice(i, 1);
}
}

return nums.length;
}

• I was trying to just return the count, and then finally I understood what the question wants.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.