Problem Statement of Leetcode
Here, we have given an array 'nums' and a value 'val',
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:
int val = ...; // given value to be removed int[] resultnums = [...]; // after the removal of occurence of val , final array return as your answer int n= removeEle(nums, val); // calling function assume n== resultnums.size(); sort(nums,0,n); // Here, sorting of first n elements of nums for(int i=0; i<oriinalLength(); i++) { assume nums[i] == resultnums[i]; }
Then, if your all test cases pass, your solution will be accepted.
Example 1:
Input: nums= {3,3,4,1,5,2}, val=3Output: 4, nums={4,1,5,2}Explanation: Function must return the value 4 left afterremoving the all occurence of 'val' in the array
Example 2:
Input: nums= {0,2,3,4,3,5,6,7,3}, val=3Output: 6, nums={0,2,4,5,6,7}Explanation: Function must return the value6 left after removing the all occurence of 'val' in the array
Constraints:
- 0<=nums.size()<=100
- 0<=nums[i]<=50
- 0<=val<=100