Skip to content

Instantly share code, notes, and snippets.

@kolharsam
Last active Aug 17, 2020
Embed
What would you like to do?
Cassidy's Interview Question - 17/08/20
package main
import "fmt"
func moveZeros(numList []int) []int {
j := len(numList) - 1
// immutability ftw!
resList := make([]int, len(numList))
copy(resList, numList)
for i, val := range numList {
if val == 0 {
resList[i] = resList[j]
resList[j] = 0
j--
}
if i >= j {
break
}
}
return resList
}
func main() {
list := []int{1, 2, 0, 1, 0, 0, 3, 6}
fmt.Println(moveZeros(list))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment