Given an integer array nums sorted in non-descending order, it will be deduplicated so that each unique element appears exactly once. The relative order of the elements should remain the same.
Some languages don't let you change the length of the array, so you need to put the result in the first part of the nums array instead. More formally, if there are k elements after removing duplicates, then the first k elements of num must contain the final result. It doesn't matter what you leave after the first k elements.
In the function, we have to return the n size of array ( first n elements remaining in the array).
We don't have to allocate another array of memory. You just have to modify that given array in place with O(1) extra memory.
As we know question statement is given an array and an integer. We have to remove all elements of the array which have the same value equal to Val and return the size of the array after operation in the function.
Custom Judge:
The compiler will check your solution with the following code:
Then, if your all test cases pass, your solution will be accepted.
Example 1:
Input: nums = [5,5,9] Output: 2, nums = [5,9,_] Explanation: Your solution must return n = 2, which are first 2 elements in array 5,9 respectively.
You don't have to worry what you left on duplicate elements, just return the array size in the end of function.
Example 2:
Input: nums = [3,3,4,4,4,5,5,6,6,7] Output: 5, nums = [3,4,5,6,7,_,_,_,_,_] Explanation: Your solution must return n = 5, which are first 5 elements in array 3,4,5,6,7 respectively. You don't have to worry what you left on duplicate elements, just return the array size in the end of function.
Constraints:
1 <= nums.size() <= 3 * 104
-100 <= nums[i] <= 100
nums
is sorted in non-decreasing order.