mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-04 16:12:47 +08:00
18 lines
249 B
Go
18 lines
249 B
Go
package leetcode
|
|
|
|
func removeElement(nums []int, val int) int {
|
|
if len(nums) == 0 {
|
|
return 0
|
|
}
|
|
j := 0
|
|
for i := 0; i < len(nums); i++ {
|
|
if nums[i] != val {
|
|
if i != j {
|
|
nums[i], nums[j] = nums[j], nums[i]
|
|
}
|
|
j++
|
|
}
|
|
}
|
|
return j
|
|
}
|