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:
nums such that the first k elements of nums contain the elements which are not equal to val. The remaining elements of nums are not important as well as the size of nums.k.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).
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).
0 <= nums.length <= 1000 <= nums[i] <= 500 <= val <= 100The problem statement clearly asks us to modify the array in-place and it also says that the element beyond the new length of the array can be anything. Given an element, we need to remove all the occurrences of it from the array. We don't technically need to remove that element per-say, right?
We can move all the occurrences of this element to the end of the array. Use two pointers!
Yet another direction of thought is to consider the elements to be removed as non-existent. In a single pass, if we keep copying the visible elements in-place, that should also solve this problem for us.
No developer has claimed the Java throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.class Solution {
public int removeElement(int[] nums, int val) {
var left = 0;
var right = nums.length - 1;
while (left <= right) {
if (nums[left] != val) {
left += 1;
continue;
}
if (nums[right] == val) {
right -= 1;
continue;
}
nums[left] = nums[right];
left += 1;
right -= 1;
}
return left;
}
}
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.class Solution {
public int removeElement(int[] nums, int val) {
var slowIndex = 0;
for (var num : nums) { // This line is the most important. You'd better memorize it.
if (num != val) {
nums[slowIndex] = num;
slowIndex += 1;
}
}
return slowIndex;
}
}
No developer has claimed the Python throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
left = 0
right = len(nums) - 1
while left <= right:
if nums[left] != val:
left += 1
continue
if nums[right] == val:
right -= 1
continue
nums[left] = nums[right]
left += 1
right -= 1
return left
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.class Solution:
def removeElement(self, nums: List[int], val: int) -> int:
slow_index = 0
for num in nums: # This line is the most important. You'd better memorize it.
if num != val:
nums[slow_index] = num
slow_index += 1
return slow_index
No developer has claimed the C++ throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.class Solution {
public:
int removeElement(vector<int>& nums, int val) {
int left = 0;
int right = nums.size() - 1;
while (left <= right) {
if (nums[left] != val) {
left += 1;
continue;
}
if (nums[right] == val) {
right -= 1;
continue;
}
nums[left] = nums[right];
left += 1;
right -= 1;
}
return left;
}
};
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.class Solution {
public:
int removeElement(vector<int>& nums, int val) {
auto slow_index = 0;
for (auto num : nums) { // This line is the most important. You'd better memorize it.
if (num != val) {
nums[slow_index] = num;
slow_index += 1;
}
}
return slow_index;
}
};
No developer has claimed the Javascript throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.var removeElement = function (nums, val) {
let left = 0
let right = nums.length - 1
while (left <= right) {
if (nums[left] != val) {
left += 1
continue
}
if (nums[right] == val) {
right -= 1
continue
}
nums[left] = nums[right]
left += 1
right -= 1
}
return left
};
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.var removeElement = function (nums, val) {
let slowIndex = 0
nums.forEach((num) => { // This line is the most important. You'd better memorize it.
if (num != val) {
nums[slowIndex] = num
slowIndex += 1
}
})
return slowIndex
};
No developer has claimed the C# throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.public class Solution
{
public int RemoveElement(int[] nums, int val)
{
int left = 0;
int right = nums.Length - 1;
while (left <= right)
{
if (nums[left] != val)
{
left += 1;
continue;
}
if (nums[right] == val)
{
right -= 1;
continue;
}
nums[left] = nums[right];
left += 1;
right -= 1;
}
return left;
}
}
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.public class Solution
{
public int RemoveElement(int[] nums, int val)
{
int slowIndex = 0;
foreach (int num in nums) // This line is the most important. You'd better memorize it.
{
if (num != val)
{
nums[slowIndex] = num;
slowIndex += 1;
}
}
return slowIndex;
}
}
No developer has claimed the Go throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.func removeElement(nums []int, val int) int {
left := 0
right := len(nums) - 1
for left <= right {
if nums[left] != val {
left += 1
continue
}
if nums[right] == val {
right -= 1
continue
}
nums[left] = nums[right]
left++
right--
}
return left
}
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.func removeElement(nums []int, val int) int {
slowIndex := 0
for _, num := range nums { // This line is the most important. You'd better memorize it.
if num != val {
nums[slowIndex] = num
slowIndex += 1
}
}
return slowIndex
}
No developer has claimed the Ruby throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
Two pointers, one on the left and one on the right. The left one points to the head of the array, and the right one points to the tail of the array.fast and slow pointers.def remove_element(nums, val)
left = 0
right = nums.size - 1
while left <= right
if nums[left] != val
left += 1
next
end
if (nums[right] == val)
right -= 1
next
end
nums[left] = nums[right]
left += 1
right -= 1
end
left
end
fast and slow pointer approach means that both pointers initially point to the head of the array, and then one pointer moves faster.two pointers of left and right.def remove_element(nums, val)
slow_index = 0
nums.each do |num| # This line is the most important. You'd better memorize it.
if num != val
nums[slow_index] = num
slow_index += 1
end
end
slow_index
end
No developer has claimed the Rust throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #27 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.