Skip to content

Instantly share code, notes, and snippets.

@ChrisGuzman
Last active April 4, 2020 22:49
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 ChrisGuzman/83b2ae1bd96687058298e1eba7077a87 to your computer and use it in GitHub Desktop.
Save ChrisGuzman/83b2ae1bd96687058298e1eba7077a87 to your computer and use it in GitHub Desktop.
4 move zeroes
fun main(args: Array<String>) {
val nums = intArrayOf(0,1,0,3,12)
moveZeroes(nums)
nums.map {
print("$it, ")
}
}
fun moveZeroes(nums: IntArray): Unit {
val zeroesCount = nums.count { it == 0 }
val correctList = nums.toMutableList().filter { it != 0 } + List(zeroesCount) {0}
nums.forEachIndexed { index, i ->
nums[index] = correctList[index]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment