> For the complete documentation index, see [llms.txt](https://blog.yushunchen.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://blog.yushunchen.com/algo/array/move-zeroes.md).

# Move Zeroes

{% embed url="<https://leetcode.com/problems/move-zeroes/>" %}

## Solution 1

```go
func moveZeroes(nums []int)  {
    for i := 0; i < len(nums)-1; i++ {
        j := i
        if nums[i] == 0 {
            for j++; j < len(nums)-1 && nums[j] == 0; j++ {}
            nums[i], nums[j] = nums[j], nums[i]
        }
    }
}
```

## Solution 2

```go
func moveZeroes(nums []int)  {
    j := 0
    for i := 0; i < len(nums); i++ {
        if nums[i] != 0 {
            if i != j {
                nums[i], nums[j] = nums[j], nums[i]
                j++
            } else {
                j++
            }
        }
    }
}
```
