Skip to content

Commit 1349ed9

Browse files
committed
using remove method
1 parent f8dbae6 commit 1349ed9

File tree

2 files changed

+3
-0
lines changed

2 files changed

+3
-0
lines changed

leet code/27.remove_elements.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,19 @@
1616
# Explanation: Your function should return k = 2, with the first two elements of nums being 2.
1717
# It does not matter what you leave beyond the returned k (hence they are underscores).
1818

19+
# using pop method
1920
def removeElement(nums, val):
2021
while val in nums:
2122
i=nums.index(val)
2223
nums.pop(i)
2324
return len(nums)
2425

26+
# using remove method
2527
def removeElement(nums, val):
2628
while val in nums:
2729
nums.remove(val) # remove first occurence only
2830
return len(nums)
2931

32+
# using list comprehension
3033
def removeElement(nums, val):
3134
nums[:] = [num for num in nums if num != val]
File renamed without changes.

0 commit comments

Comments
 (0)