Last active
April 4, 2020 22:49
-
-
Save ChrisGuzman/83b2ae1bd96687058298e1eba7077a87 to your computer and use it in GitHub Desktop.
4 move zeroes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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