Given an integer array
Consider the number of elements in
-
Change the array
nums such that the firstk elements ofnums contain the elements which are not equal toval . The remaining elements ofnums are not important as well as the size ofnums . -
Return
k .
Custom Judge:
The judge will test your solution with the following code:
If all assertions pass, then your solution will be accepted.
Output: 2, nums = [2,2,_,_]
Explanation: Your function should return k = 2, with the first two elements of nums being 2. It does not matter what you leave beyond the returned k (hence they are underscores).
Output: 5, nums = [0,1,4,0,3,_,_,_]
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4. Note that the five elements can be returned in any order. It does not matter what you leave beyond the returned k (hence they are underscores).
Constraints:
0 <= nums.length <= 100 0 <= nums[i] <= 50 0 <= val <= 100
Contents
Solution using in-place removal of elements using two pointers
In this approach, we are going to use in-place removal of elements using two pointers.
-
We will create a function
inPlaceRemovalUsingTwoPointers that takes input argumentsnums andval . -
We are then going to create a variable
count that we are going use to count how many elements are left after removal of elements whose value is equal toval , we are also going to use this as one of the pointer in this two pointers approach. -
Then, for each element at index
i check if the value is equal toval , if it is not equal then copy element at indexi to indexcount and incrementcount .
-
Initially both
count andi are going to be 0. -
When we are at 1st element 1, we will not do anything since it is the
val to be removed and we movei to 1. -
When we are at 2nd element 1, we will not do anything at this element either and move
i to 2. -
When we are at 3rd element 2, since this is not equal to the element that we want to remove, we will copy this to
count index and incrementcounter , so it becomes 1. Now the resultant array looks like this[2, 1, 2, 3] -
When we are at 4th element 3, since this is not equal to the element that we want to remove, we will copy this to
count index and incrementcounter , so it becomes 2. Now the resultant array looks like this[2, 3, 2, 3] -
If you observer,
count holds, how many elements were left which are not equal toval after it is removed and all the values that are not equal to are moved to the beginning of array.
In short, we have moved elements to the left that are not equal toval and left other elements as they are not needed to be looked at, as per problem statement. (Incase if theval need to be moved to right side, then we can swap elements instead just copying) -
Finally, just return
count , which holds how many elements are left after removal ofval .
The reason why we do this is because, consider an example with input array as
Complexity Analysis:
Time complexity: Above code runs in O(n) time where
Space complexity: O(1).
Above implementations source code can be found at