Introduction 🚀
In this classic LeetCode problem, you’ll be working with arrays in a practical way to remove specific values in-place while maintaining the order of remaining elements.
Problem Statement đź“ť
You are given an integer array nums
and an integer val
. Remove all occurrences of val
from nums
in-place and return the number of elements remaining that are not equal to val
.
Custom Requirements:
- Modify the input array such that the first
k
elements contain values not equal toval
. - The remaining elements in the array beyond
k
are irrelevant. - Return
k
, the count of elements not equal toval
.
đź’ˇExamples
Example 1
Input:
nums = [3,2,2,3], val = 3
Output:
k = 2, nums = [2,2,_,_]
Explanation: The first two elements of nums
are updated to [2,2]
, ignoring the rest.
Example 2
Input:
nums = [0,1,2,2,3,0,4,2], val = 2
Output:
k = 5, nums = [0,1,4,0,3,_,_,_]
Explanation: After removing all instances of 2
, we are left with five valid elements in the array.
Approch đź§
To solve this problem efficiently:
- Use two pointers:
- A slow pointer to track the position where the next valid element will go.
- A fast pointer to iterate through the array.
- If the current element is not equal to
val
, move it to the position tracked by the slow pointer and increment the slow pointer. - Continue until the fast pointer reaches the end of the array.
Java Solutionđź’ˇ
Here’s the implementation:
class Solution {
public int removeElement(int[] nums, int val) {
int k = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != val) {
nums[k] = nums[i];
k++;
}
}
return k;
}
}
Explanation of Code 🔍
-
Initialization:
-
k
is initialized to0
, representing the position of the next valid element.
-
-
Iteration:
Traverse through the array with a loop:- If
nums[i]
is not equal toval
, update thenums[k]
position and incrementk
.
- If
Output:
Return the value ofk
as the count of valid elements in the array.
Complexity Analysis 🔧
Time Complexity:
O(n)
, wheren
is the length of the array. The algorithm iterates through the array once.Space Complexity:
O(1)
, as the solution modifies the array in place without using additional space.
Step-by-Step Execution
Input: nums = [0,1,2,2,3,0,4,2]
, val = 2
- Start:
k = 0
- Iteration:
Output:
k = 5
,nums = [0,1,3,0,4,_,_,_]
Conclusion ✨
The Remove Element problem is a classic array manipulation challenge that demonstrates the power of two-pointer techniques for in-place modifications. By iterating through the array just once, we can efficiently remove all occurrences of a target value while keeping the remaining elements intact.
Key takeaways 🎯
- Time efficiency: The solution has a linear time complexity of
O(n)
, making it optimal for the given constraints. - Space efficiency: No additional data structures are used, ensuring constant space usage
(O(1))
. - Real-world relevance: Similar techniques are used in scenarios like filtering data streams, cleaning up lists, or processing arrays with constraints.
With its simplicity and practical utility, this problem helps solidify your understanding of basic array manipulation and pointer-based algorithms—critical skills for coding interviews and problem-solving in real-world scenarios.
Top comments (1)
Follow Me on GitHub 🚀
If you found this solution helpful, check out more of my projects and solutions on my GitHub profile.
Don't forget to follow for more updates!