Remove Element Java Solution
Problem :
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.
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
Solution:
public int removeElement(int[] nums, int val) {
int count =0;
int i =0;
while(i<nums.length) {
if(nums[i] != val) {
nums[count++] = nums[i];
}
++i;
}
return count;
}
0 Comments:
Post a Comment