Skip to content

Instantly share code, notes, and snippets.

@kolharsam
Last active August 17, 2020 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kolharsam/b8879ca3770af2f3441345845a1be3b3 to your computer and use it in GitHub Desktop.
Save kolharsam/b8879ca3770af2f3441345845a1be3b3 to your computer and use it in GitHub Desktop.
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