Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c3b51f1

Browse files
author
Saidev
committed
Added "Remove Element" problem
1 parent 66d1664 commit c3b51f1

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

‎03-Remove Element.py‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
Example 1:
3+
4+
Input: nums = [3,2,2,3], val = 3
5+
Output: 2, nums = [2,2,_,_]
6+
Explanation: Your function should return k = 2, with the first two elements of nums being 2.
7+
It does not matter what you leave beyond the returned k (hence they are underscores).
8+
9+
Example 2:
10+
11+
Input: nums = [0,1,2,2,3,0,4,2], val = 2
12+
Output: 5, nums = [0,1,4,0,3,_,_,_]
13+
Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
14+
Note that the five elements can be returned in any order.
15+
It does not matter what you leave beyond the returned k (hence they are underscores).
16+
'''
17+
18+
def removeElement(nums, val):
19+
for i in range(nums.count(val)):
20+
nums.pop(nums.index(val))
21+
return len(nums)
22+
23+
nums = [3,2,2,3]
24+
print(removeElement(nums, 3))
25+
26+
nums = [0,1,2,2,3,0,4,2]
27+
print(removeElement(nums, 2))
28+
29+
nums = []
30+
print(removeElement(nums, 3))

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /