Skip to content

Commit ad293fa

Browse files
authored
Merge pull request #92 from allwells/allwells
Solution for question 27
2 parents b93ca38 + 75e47b1 commit ad293fa

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

0027/README.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 27. Remove Elements
2+
3+
## Problem Statement
4+
5+
Given an integer array `nums` and an integer `val`, remove all occurrences of `val` in `nums` in-place. The relative order of the elements may be changed.
6+
7+
Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array `nums`. More formally, if there are `k` elements after removing the duplicates, then the first `k` elements of `nums` should hold the final result. It does not matter what you leave beyond the first `k` elements.
8+
9+
Return `k` after placing the final result in the first `k` slots of `nums`.
10+
11+
Do not allocate extra space for another array. You must do this by modifying the input array in-place with O(1) extra memory.
12+
13+
## Link to problem
14+
15+
<https://leetcode.com/problems/remove-element/>

0027/RemoveElements.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
6+
def removeElement(self, nums: List[int], val: int) -> int:
7+
k = 0
8+
9+
for n in range(len(nums)):
10+
11+
# checks which element in the array does not match with val
12+
if nums[n] != val:
13+
nums[k] = nums[n]
14+
k += 1 # increments the value of k by 1
15+
16+
return k

0 commit comments

Comments
 (0)