Description:
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
Consider the number of elements in nums
which are not equal to val
be k
, to get accepted, you need to do the following things:
- 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
.
Examples:
Example 1:
Input: nums = [3,2,2,3], val = 3
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).
Example 2:
Input: nums = [0,1,2,2,3,0,4,2], val = 2
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).
Solution in Python:
Python
class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
# Initialize a pointer to keep track of the position to place elements not equal to val
write_index = 0
# Iterate over each element in the array
for i in range(len(nums)):
# If the current element is not equal to val
if nums[i] != val:
# Place it at the write_index position
nums[write_index] = nums[i]
# Move the write_index forward
write_index += 1
# The write_index now represents the number of elements not equal to val
return write_index
# Example usage:
# nums = [3, 2, 2, 3]
# val = 3
# solution = Solution()
# k = solution.removeElement(nums, val)
# print(k) # Output: 2
# print(nums[:k]) # Output: [2, 2]
# nums = [0, 1, 2, 2, 3, 0, 4, 2]
# val = 2
# solution = Solution()
# k = solution.removeElement(nums, val)
# print(k) # Output: 5
# print(nums[:k]) # Output: [0, 1, 3, 0, 4]
Explanation:
- Initialization:
- We initialize a
write_index
pointer to0
. This pointer keeps track of where to place the next element that is not equal toval
.
- We initialize a
- Iterating through the Array:
- We iterate through each element in the
nums
array using afor
loop. - For each element, we check if it is not equal to
val
. - If the element is not equal to
val
, it means we need to keep this element.
- We iterate through each element in the
- Placing Non-
val
Elements:- We place the non-
val
element at the position indicated bywrite_index
. - We then increment
write_index
to move to the next position for the next non-val
element.
- We place the non-
- Returning the Result:
- After the loop completes,
write_index
will indicate the number of elements that are not equal toval
. - This is the value we return, and the first
write_index
elements ofnums
will contain the elements that are not equal toval
.
- After the loop completes,
Solution in Javascript:
JavaScript
/**
@param {number[]} nums
@param {number} val
@return {number}
*/
var removeElement = function(nums, val) {
// Initialize a pointer to keep track of the position to place elements not equal to val
let writeIndex = 0;
// Iterate over each element in the array
for (let i = 0; i < nums.length; i++) {
// If the current element is not equal to val
if (nums[i] !== val) {
// Place it at the writeIndex position
nums[writeIndex] = nums[i];
// Move the writeIndex forward
writeIndex++;
}
}
// The writeIndex now represents the number of elements not equal to val
return writeIndex;
};
// Example usage:
let nums1 = [3, 2, 2, 3];
let val1 = 3;
let k1 = removeElement(nums1, val1);
console.log(k1); // Output: 2
console.log(nums1.slice(0, k1)); // Output: [2, 2]
let nums2 = [0, 1, 2, 2, 3, 0, 4, 2];
let val2 = 2;
let k2 = removeElement(nums2, val2);
console.log(k2); // Output: 5
console.log(nums2.slice(0, k2)); // Output: [0, 1, 3, 0, 4]
Solution in Java:
Java
class Solution {
public int removeElement(int[] nums, int val) {
// Initialize a pointer to keep track of the position to place elements not equal to val
int writeIndex = 0;
// Iterate over each element in the array
for (int i = 0; i < nums.length; i++) {
// If the current element is not equal to val
if (nums[i] != val) {
// Place it at the writeIndex position
nums[writeIndex] = nums[i];
// Move the writeIndex forward
writeIndex++;
}
}
// The writeIndex now represents the number of elements not equal to val
return writeIndex;
}
// Example usage
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums1 = { 3, 2, 2, 3 };
int val1 = 3;
int k1 = solution.removeElement(nums1, val1);
System.out.println(k1); // Output: 2
for (int i = 0; i < k1; i++) {
System.out.print(nums1[i] + " "); // Output: 2 2
}
System.out.println();
int[] nums2 = { 0, 1, 2, 2, 3, 0, 4, 2 };
int val2 = 2;
int k2 = solution.removeElement(nums2, val2);
System.out.println(k2); // Output: 5
for (int i = 0; i < k2; i++) {
System.out.print(nums2[i] + " "); // Output: 0 1 3 0 4
}
System.out.println();
}
}
Solution in C#:
C#
public class Solution {
public int RemoveElement(int[] nums, int val) {
// Initialize a pointer to keep track of the position to place elements not equal to val
int writeIndex = 0;
// Iterate over each element in the array
for (int i = 0; i < nums.Length; i++) {
// If the current element is not equal to val
if (nums[i] != val) {
// Place it at the writeIndex position
nums[writeIndex] = nums[i];
// Move the writeIndex forward
writeIndex++;
}
}
// The writeIndex now represents the number of elements not equal to val
return writeIndex;
}
}