🧠
Two Pointers - Swap with End Approach
💡 This approach is useful when the order of elements does not need to be preserved. It reduces the number of writes by swapping unwanted elements with the last element, shrinking the array from the end.
Intuition
Use two pointers: one at the start and one at the end. When the start pointer finds val, swap it with the end pointer's element and reduce the end pointer. Otherwise, move the start pointer forward.
Algorithm
- Initialize start pointer at 0 and end pointer at last index.
- While start <= end, if nums[start] == val, swap with nums[end] and decrement end.
- If nums[start] != val, increment start.
- Return start as the new length.
💡 This approach minimizes writes by swapping only when necessary and avoids shifting.
def remove_element(nums, val):
start, end = 0, len(nums) - 1
while start <= end:
if nums[start] == val:
nums[start], nums[end] = nums[end], nums[start]
end -= 1
else:
start += 1
return start
# Driver code
if __name__ == '__main__':
arr = [3,2,2,3]
val = 3
new_len = remove_element(arr, val)
print(new_len, arr[:new_len])
Line Notes
start, end = 0, len(nums) - 1Initialize two pointers at start and end
while start <= endLoop until pointers cross
if nums[start] == valCheck if start pointer points to val to remove
nums[start], nums[end] = nums[end], nums[start]Swap val with element at end to remove it
public class RemoveElement {
public static int removeElement(int[] nums, int val) {
int start = 0, end = nums.length - 1;
while (start <= end) {
if (nums[start] == val) {
int temp = nums[start];
nums[start] = nums[end];
nums[end] = temp;
end--;
} else {
start++;
}
}
return start;
}
public static void main(String[] args) {
int[] arr = {3,2,2,3};
int val = 3;
int newLen = removeElement(arr, val);
System.out.print(newLen + " ");
for (int i = 0; i < newLen; i++) System.out.print(arr[i] + " ");
}
}
Line Notes
int start = 0, end = nums.length - 1;Initialize two pointers at start and end
while (start <= end)Loop until pointers cross
if (nums[start] == val)Check if start pointer points to val
Swap elements at start and endRemove val by swapping with end element
#include <iostream>
#include <vector>
using namespace std;
int removeElement(vector<int>& nums, int val) {
int start = 0, end = nums.size() - 1;
while (start <= end) {
if (nums[start] == val) {
swap(nums[start], nums[end]);
end--;
} else {
start++;
}
}
return start;
}
int main() {
vector<int> arr = {3,2,2,3};
int val = 3;
int newLen = removeElement(arr, val);
cout << newLen << " ";
for (int i = 0; i < newLen; i++) cout << arr[i] << " ";
return 0;
}
Line Notes
int start = 0, end = nums.size() - 1;Initialize two pointers at start and end
while (start <= end)Loop until pointers cross
if (nums[start] == val)Check if start pointer points to val
swap(nums[start], nums[end]);Swap val with element at end to remove it
function removeElement(nums, val) {
let start = 0, end = nums.length - 1;
while (start <= end) {
if (nums[start] === val) {
[nums[start], nums[end]] = [nums[end], nums[start]];
end--;
} else {
start++;
}
}
return start;
}
// Test
const arr = [3,2,2,3];
const val = 3;
const newLen = removeElement(arr, val);
console.log(newLen, arr.slice(0, newLen));
Line Notes
let start = 0, end = nums.length - 1;Initialize two pointers at start and end
while (start <= end)Loop until pointers cross
if (nums[start] === val)Check if start pointer points to val
[nums[start], nums[end]] = [nums[end], nums[start]];Swap val with element at end to remove it
Each element is visited at most once by start pointer and swapped at most once, resulting in linear time.
💡 For n=20, this means about 20 operations, efficient and minimal writes.
Interview Verdict: Accepted and optimal when order does not matter
This approach is preferred if element order preservation is not required, reducing writes.