1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| fun removeDuplicates(nums: IntArray): Int { if (nums.isEmpty()) return 0 var i = 0 for (j in 1 until nums.size) { if (nums[j] != nums[i]) { i++ nums[i] = nums[j] } } return i + 1 }
fun main() { val nums = intArrayOf(1, 1, 2, 2, 3) val length = removeDuplicates(nums) println("新的数组长度: $length") for (i in 0 until length) { print("${nums[i]} ") } }
|