Remove Element
You get an array of numbers and one number, val. Get rid of every copy of val inside the same array, without a second one, and return how many numbers are left. Those numbers must fill the start of the array, in any order; what lies after them does not matter. Our example: [4, 1, 4, 4, 2, 3], val = 4 → 3, and the array starts with [1, 2, 3].
- input
nums: integers;val: the number to remove.- returns
k, how many numbers ofnumsare notval. Changenumsin place so that its firstkitems are those numbers, in any order; what is left after them does not matter.- constraints
- 0 ≤ nums.length ≤ 100
- 0 ≤ nums[i] ≤ 50
- 0 ≤ val ≤ 100
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @param {number} val
* @return {number}
*/
function removeElement(nums, val) {
}
Ctrl ↵
format
reset
saved in this browser
Stuck?
See the solution run step by step on our example, then why it works and its complexity.
Next: more two pointers problems
- 125Valid PalindromeEasysolve
- 26Remove Duplicates from Sorted ArrayEasysolve
- 283Move ZeroesEasysolve
- 88Merge Sorted ArrayEasysolve
- 392Is SubsequenceEasysolve
- 167Two Sum II - Input Array Is SortedMediumsolve
- 153SumMediumsolve
- 11Container With Most WaterMediumsolve
- 80Remove Duplicates from Sorted Array IIMediumsolve
- 189Rotate ArrayMediumsolve
- 151Reverse Words in a StringMediumsolve
- 5Longest Palindromic SubstringMediumsolve
- 42Trapping Rain WaterHardsolve
The problem and its statement belong to LeetCode. Here are our own retelling, example and solution. Patterns Hero is not affiliated with LeetCode.