Algorithmsfiltering
Two Pointer with Filtering
TT
Testlaa Team
May 14, 2026•1 min read
Filtering removes elements matching a rule while scanning once: move valid elements to the front with a write pointer.
Try this in Python
def remove_value(nums: list[int], val: int) -> int:
w = 0
for x in nums:
if x != val:
nums[w] = x
w += 1
return w
a = [3, 2, 2, 3]
k = remove_value(a, 3)
print(k, a[:k])
Key takeaways
- In-place filtering uses read/write pointers.
- Stable order preserved here.
- Return new logical length.
Tags:
Two pointersPythonStudents
