Remove Duplicates from Sorted Array
You get a non-empty array of numbers sorted in ascending order. Leave each number once at the start of the same array, in the same order, without a second array, and return how many there are. What lies after them does not matter. Our example: [1, 1, 2, 3, 3, 3, 5] → 4, with [1, 2, 3, 5] at the start.
- input
nums: integers in non-decreasing order, so equal ones stand together.- returns
k, the number of different numbers innums. Changenumsin place so that its firstkitems are those numbers, each once, in their order; what is left after them does not matter.- constraints
- 1 ≤ nums.length ≤ 3 · 10⁴
- −100 ≤ nums[i] ≤ 100
- nums is in non-decreasing order
- examples
- Open a test in the tests panel: it shows the input and the expected answer.
/**
* @param {number[]} nums
* @return {number}
*/
function removeDuplicates(nums) {
}
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
- 283Move ZeroesEasysolve
- 88Merge Sorted ArrayEasysolve
- 27Remove ElementEasysolve
- 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.