Skip to content

Instantly share code, notes, and snippets.

@AbhiAgarwal192
Created August 21, 2020 04:34
Show Gist options
  • Save AbhiAgarwal192/b2de192f74df1734dbd0e5226e037cf7 to your computer and use it in GitHub Desktop.
Save AbhiAgarwal192/b2de192f74df1734dbd0e5226e037cf7 to your computer and use it in GitHub Desktop.
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
public class Solution {
public int RemoveElement(int[] nums, int val) {
int len = nums.Length;
int i = 0;
int j = 0;
while(i<len){
while(i<len && nums[i] == val){
i++;
}
if(i<len){
nums[j] = nums[i];
j++;
}
i++;
}
return j;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment