Shift Zeros to the End
Given an integer array nums, move all zeros to the end while maintaining the relative order of the non-zero elements. Do this in-place.
Input: nums = [0,1,0,3,12]
Output: [1,3,12,0,0]
export function shiftZerosToEnd(nums: number[]): void {
let write = 0;
for (let read = 0; read < nums.length; read++) {
if (nums[read] !== 0) {
[nums[write], nums[read]] = [nums[read], nums[write]];
write++;
}
}
}