Skip to content

Commit 5547ba0

Browse files
Create 0027-Remove-Element.swift
1 parent 2b1ef04 commit 5547ba0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

swift/0027-Remove-Element.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
3+
// keep track of the count, init to 0
4+
var count: Int = 0
5+
6+
// (for) go through nums
7+
// if nums[i] !- val
8+
// set nums[count] = nums[i]
9+
// increment count by 1
10+
for i in nums {
11+
if i != val {
12+
nums[count] = i
13+
count += 1
14+
}
15+
}
16+
17+
// return count
18+
return count
19+
}
20+
}

0 commit comments

Comments
 (0)