Skip to content

Commit

Permalink
add problem #27 remove element
Browse files Browse the repository at this point in the history
  • Loading branch information
brownbeardeveloper committed Dec 4, 2024
1 parent f9b8665 commit 0037715
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions remove_element.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def remove_element(nums: list[int], val: int) -> int:
write_index, read_index = 0, 0

while read_index <= len(nums)-1:
if nums[read_index] != val:
nums[write_index], nums[read_index] = nums[read_index], nums[write_index]
write_index +=1
read_index +=1
else:
nums[read_index] = "_"
read_index +=1
return write_index

if __name__ == "__main__":
nums_list = [0,1,2,2,3,0,4,2]
result = Solution.remove_element(nums=nums_list, val=2)
print(result) # output: 5
print(nums_list) # output: [0, 1, 3, 0, 4, '_', '_', '_']

0 comments on commit 0037715

Please sign in to comment.