Skip to main content
Back to problems
#283
Easy Algorithms

Move zeroes

Array Two Pointers
63.6% acceptance
Jan 12, 2026
19264
590
Given an integer array nums, move all 0's to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn move_zeroes(nums: &mut Vec<i32>) {
    let mut write_pos = 0;
    
    for read_pos in 0..nums.len() {
      if nums[read_pos] != 0 {
        nums[write_pos] = nums[read_pos];
        write_pos += 1;
      }
    }
    
    for i in write_pos..nums.len() {
      nums[i] = 0;
    }
  }
}