Algorithm Day113 - Remove Element
π Day113 - LeetCode 27: Remove Element
π Problem
Given an integer array nums and an integer val, remove all occurrences of val in nums in-place.
Return the number of remaining elements. Order may change.
π§ Approach
- Use two pointers
fastscans arrayslowrecords valid element positions- When
nums[fast] != val, copy tonums[slow]
β Complexity
- Time: O(n)
- Space: O(1)
π» Java Code
1 | class Solution { |
π Notes
- We donβt need an extra array
- Just overwrite values in-place
slowpointer gives the new length
Keep grinding β one step closer every day! πͺπ₯