Given an array nums and a value val
, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1)
extra memory.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
See full problem description on LeetCode :-
Lets see solutions :-
Solution 1 :-
class Solution {
public int removeElement(int[] nums, int val) {
int temp = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[temp] = nums[i];
temp++;
}
}
return temp;
}
}
Input and Output :-
Input
nums = [3,2,2,3], val = 3Output
2, nums = [2,2]___________________
Input
nums = [0,1,2,2,3,0,4,2], val = 2Output
5, nums = [0,1,4,0,3]
Explanation :-
We have to remove given value from array.
- Initialize any temporary variable with 0.
- Loop through given array nums.
- In if condition check given val is not matching with current array value.
- If val is not match with current array element, then assign nums[i] array element to current array element
- nums [3, 2, 2, 3], val = 3
- i = 0, temp = 0.
- 0 is less than array length 4, so it goes in loop.
- nums[0] != 3 | 3 != 3 | false. it does not go into if loop.
- i = 1, temp = 0.
- nums[1] != 3 | 2 != 3 | true. it goes into if loop
- nums[temp] = nums[i] | nums[0] = nums[1] | assign 1st element to 0th element into nums | nums [2, 2, 2, 3]
- temp++ | 1
- i = 2, temp = 1, numa = [2, 2, 2, 3]
- nums[2] != 3 | 2 != 3 | true. it goes into if loop
- nums[temp] = nums[i] | nums[1] = nums[2] | assign 2st element to 1th element into nums | nums [2, 2, 2, 3]
- temp++ | 2
- i = 3, temp = 2, numa = [2, 2, 2, 3]
- nums[3] != 3 | 3 != 3 | false. it does not goes into if loop.
- i = 4, temp = 2, numa = [2, 2, 2, 3]
- Now i become 4, so i does goes into fro loop. and we got answer.
- It doesn't matter what you leave beyond the returned length. For example if you return 2 with nums = [2,2,3,3] or nums = [2,2,0,0], your answer will be accepted.
(From leetcode).
Solution 2 :-
public int removeElement(int[] nums, int val) {
int i = 0;
int length = nums.length;
while (i < length) {
if (nums[i] == val) {
// assign last array element to current array position
nums[i] = nums[length - 1];
// reduce array size by one
length--;
} else {
i++;
}
}
return length;
}
In above solution we simply assign last array element to current array index.
Explanation :-
- nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 8, i = 0
- First two time nums[i] == val is false so it goes to else condition and increment i value.
- nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 8, i = 2
- nums[i] == val | 2 == 2 becomes true and goes in if condition.
- nums[i] = nums[length - 1] | nums[2] = nums[7]. So nums array last element is 2 and we are copy 2 to current index 2. and decrement length by 1.
- nums[0, 1, 2, 2, 3, 0, 4, 2], val = 2, length = 7, i = 2
- nums[i] == val | 2 == 2 becomes true and goes in if condition.
- nums[i] = nums[length - 1] | nums[2] = nums[6]. So nums array last element is 4 and we are copy 4 to current index 2. and decrement length by 1.
- nums[0, 1, 4, 2, 3, 0, 4, 2], val = 2, length = 6, i = 2
- nums[0, 1, 4, 2, 3, 0, 4, 2], val = 2, length = 6, i = 3
- nums[i] == val | 2 == 2 becomes true and goes in if condition.
- nums[i] = nums[length - 1] | nums[3] = nums[5]. So nums array last element is 0 and we are copy 0 to current index 3. and decrement length by 1.
- nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 3
- nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 4
- nums[0, 1, 4, 0, 3, 0, 4, 2], val = 2, length = 5, i = 5
- And now, i < length | 5 < 5 becoms false and return length that is our answer.
Happy Coding.
Other articles you may like :-
Comments
Post a Comment