Remove Duplicates Sorted Array II Problem
Remove Duplicates Sorted Array II Problem — ExecCode Easy DSA Practice
Solve the Remove Duplicates Sorted Array II problem on ExecCode. Free online easy DSA practice in Two Pointers. Write and run code in Java, C++, Python — no signup required to run.
Problem description
Description You are given an integer array nums sorted in non-decreasing order, remove some duplicates in-place such that each unique element appears at most twice. Your task is to compute the required answer and return k after placing the final result in the first k slots of nums. The relative order of the elements should be kept the same. 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.
Examples
Input {"nums": [1, 1, 1, 2, 2, 3]}; Output 5. Input {"nums": [0, 0, 1, 1, 1, 1, 2, 3, 3]}; Output 7. Input {"nums": [1, 2]}; Output 2
Constraints
1 <= nums.length <= 3 * 10^4 -10^4 <= nums[i] <= 10^4 nums is sorted in non-decreasing order.
Practice Remove Duplicates Sorted Array II free on ExecCode. Browse DSA problems, topic map, and placement guides.