From 98119a582a6f2d80bb5ab3500ffd1ef5c9ac3905 Mon Sep 17 00:00:00 2001 From: Aesha-4326 Date: Wed, 20 May 2026 22:12:13 +0530 Subject: [PATCH] upload remove element code in java --- .../0027.Remove Element/README_EN.md | 251 +----------------- 1 file changed, 10 insertions(+), 241 deletions(-) diff --git a/solution/0000-0099/0027.Remove Element/README_EN.md b/solution/0000-0099/0027.Remove Element/README_EN.md index 6bbc22e167933..5e148d2417e9d 100644 --- a/solution/0000-0099/0027.Remove Element/README_EN.md +++ b/solution/0000-0099/0027.Remove Element/README_EN.md @@ -1,247 +1,16 @@ ---- -comments: true -difficulty: Easy -edit_url: https://github.com/doocs/leetcode/edit/main/solution/0000-0099/0027.Remove%20Element/README_EN.md -tags: - - Array - - Two Pointers ---- - - - -# [27. Remove Element](https://leetcode.com/problems/remove-element) - -[中文文档](/solution/0000-0099/0027.Remove%20Element/README.md) - -## Description - - - -

Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.

- -

Consider the number of elements in nums which are not equal to val be k, to get accepted, you need to do the following things:

- - - -

Custom Judge:

- -

The judge will test your solution with the following code:

- -
-int[] nums = [...]; // Input array
-int val = ...; // Value to remove
-int[] expectedNums = [...]; // The expected answer with correct length.
-                            // It is sorted with no values equaling val.
-
-int k = removeElement(nums, val); // Calls your implementation
-
-assert k == expectedNums.length;
-sort(nums, 0, k); // Sort the first k elements of nums
-for (int i = 0; i < actualLength; i++) {
-    assert nums[i] == expectedNums[i];
-}
-
- -

If all assertions pass, then your solution will be accepted.

- -

 

-

Example 1:

- -
-Input: nums = [3,2,2,3], val = 3
-Output: 2, nums = [2,2,_,_]
-Explanation: Your function should return k = 2, with the first two elements of nums being 2.
-It does not matter what you leave beyond the returned k (hence they are underscores).
-
- -

Example 2:

- -
-Input: nums = [0,1,2,2,3,0,4,2], val = 2
-Output: 5, nums = [0,1,4,0,3,_,_,_]
-Explanation: Your function should return k = 5, with the first five elements of nums containing 0, 0, 1, 3, and 4.
-Note that the five elements can be returned in any order.
-It does not matter what you leave beyond the returned k (hence they are underscores).
-
- -

 

-

Constraints:

- - - - - -## Solutions - - - -### Solution 1: One Pass - -We use the variable $k$ to record the number of elements that are not equal to $val$. - -Traverse the array $nums$, if the current element $x$ is not equal to $val$, then assign $x$ to $nums[k]$, and increment $k$ by $1$. - -Finally, return $k$. - -The time complexity is $O(n)$ and the space complexity is $O(1)$, where $n$ is the length of the array $nums$. - - - -#### Python3 - -```python -class Solution: - def removeElement(self, nums: List[int], val: int) -> int: - k = 0 - for x in nums: - if x != val: - nums[k] = x - k += 1 - return k -``` - -#### Java - -```java class Solution { public int removeElement(int[] nums, int val) { - int k = 0; - for (int x : nums) { - if (x != val) { - nums[k++] = x; - } - } - return k; - } -} -``` - -#### C++ - -```cpp -class Solution { -public: - int removeElement(vector& nums, int val) { - int k = 0; - for (int x : nums) { - if (x != val) { - nums[k++] = x; - } - } - return k; - } -}; -``` - -#### Go - -```go -func removeElement(nums []int, val int) int { - k := 0 - for _, x := range nums { - if x != val { - nums[k] = x - k++ - } - } - return k -} -``` - -#### TypeScript - -```ts -function removeElement(nums: number[], val: number): number { - let k: number = 0; - for (const x of nums) { - if (x !== val) { - nums[k++] = x; - } - } - return k; -} -``` - -#### Rust - -```rust -impl Solution { - pub fn remove_element(nums: &mut Vec, val: i32) -> i32 { - let mut k = 0; - for i in 0..nums.len() { - if nums[i] != val { - nums[k] = nums[i]; - k += 1; - } - } - k as i32 - } -} -``` - -#### JavaScript - -```js -/** - * @param {number[]} nums - * @param {number} val - * @return {number} - */ -var removeElement = function (nums, val) { - let k = 0; - for (const x of nums) { - if (x !== val) { - nums[k++] = x; - } - } - return k; -}; -``` - -#### C# - -```cs -public class Solution { - public int RemoveElement(int[] nums, int val) { - int k = 0; - foreach (int x in nums) { - if (x != val) { - nums[k++] = x; - } - } - return k; - } -} -``` - -#### PHP - -```php -class Solution { - /** - * @param Integer[] $nums - * @param Integer $val - * @return Integer - */ - function removeElement(&$nums, $val) { - for ($i = count($nums) - 1; $i >= 0; $i--) { - if ($nums[$i] == $val) { - array_splice($nums, $i, 1); + int i = 0; + int n = nums.length; + + while (i < n) { + if (nums[i] == val) { + nums[i] = nums[n - 1]; + n--; + } else { + i++; } } + return n; } } -``` - - - - - -