LeetCode - Algorithms - 27. Remove Element

Problem

27. Remove Element

javascript

Array.prototype.splice()

1

© 9 Ways to Remove Elements From A JavaScript Array - Plus How to Safely Clear JavaScript Arrays - Removing Array Items By Value Using Splice

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
for(var i = 0; i < nums.length; i++){
if ( nums[i] === val) {
nums.splice(i, 1);
i--;
}
}
return nums.length;
};

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 76 ms, faster than 85.28% of JavaScript online submissions for Remove Element.
  • Memory Usage: 38.6 MB, less than 69.26% of JavaScript online submissions for Remove Element.

2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
var index=nums.indexOf(val);
while (index !== -1) {
index = nums.indexOf(val);
if (index!==-1)
nums.splice(index, 1);
}
return nums.length;
};

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 80 ms, faster than 65.29% of JavaScript online submissions for Remove Element.
  • Memory Usage: 38.6 MB, less than 48.89% of JavaScript online submissions for Remove Element.

3

© The Fastest Way to Remove a Specific Item from an Array in JavaScript

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
let index
while((index = nums.indexOf(val)) > -1)
{
nums.splice(index, 1)
}
return nums.length;
};

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 80 ms, faster than 65.29% of JavaScript online submissions for Remove Element.
  • Memory Usage: 38.9 MB, less than 18.66% of JavaScript online submissions for Remove Element.

Two Pointers

Translation of © Solution - Approach 1: Two Pointers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
var removeElement = function(nums, val) {
var i=0;
for(var j=0;j<nums.length;j++) {
if (nums[j]!=val) {
nums[i] = nums[j];
i++;
}
}
return i;
};

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 76 ms, faster than 85.28% of JavaScript online submissions for Remove Element.
  • Memory Usage: 38.7 MB, less than 48.89% of JavaScript online submissions for Remove Element.

Java

Two Pointers

1

© Solution - Approach 1: Two Pointers

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public int removeElement(int[] nums, int val) {
int i = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[j] != val) {
nums[i] = nums[j];
i++;
}
}
return i;
}
}

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Element.
  • Memory Usage: 37.6 MB, less than 48.38% of Java online submissions for Remove Element.

2

© Solution - Approach 2: Two Pointers - when elements to remove are rare

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public int removeElement(int[] nums, int val) {
int n = nums.length;
for (int i = 0; i < n; ) {
if (nums[i] == val) {
nums[i] = nums[n - 1];
n--;
} else
i++;
}
return n;
}
}

Submission Detail

  • 113 / 113 test cases passed.
  • Runtime: 0 ms, faster than 100.00% of Java online submissions for Remove Element.
  • Memory Usage: 37.7 MB, less than 48.38% of Java online submissions for Remove Element.