Skip to content

Instantly share code, notes, and snippets.

@daifu
Created May 29, 2013 17:08
Show Gist options
  • Save daifu/5671955 to your computer and use it in GitHub Desktop.
Save daifu/5671955 to your computer and use it in GitHub Desktop.
Remove Element
/*
Given an array and a value, remove all instances of that value in place and return the new length.
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
*/
public class Solution {
public int removeElement(int[] A, int elem) {
// Start typing your Java solution below
// DO NOT write main() function
int cur = 0;
for(int i = 0; i < A.length; i++) {
if(A[i] != elem) {
A[cur] = A[i];
cur++;
}
}
return cur;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment