27. Remove Element ๐งน
Difficulty: Easy
- Tags: Array
, Two Pointers
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
to be k
. To get accepted, you need to do the following things:
Change the array
nums
such that the firstk
elements ofnums
contain the elements which are not equal toval
. The remaining elements ofnums
are not important, as well as the size ofnums
.Return
k
.
Custom Judge ๐งช
The judge will test your solution with the following code:
If all assertions pass, then your solution will be accepted.
Examples ๐
Example 1:
Input:
Output:
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:
Output:
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 โ๏ธ
0 <= nums.length <= 100
0 <= nums[i] <= 50
0 <= val <= 100
Solution ๐ก
Java
You can find the full Solution.java
file here.
Last updated