Created
May 29, 2013 17:08
-
-
Save daifu/5671955 to your computer and use it in GitHub Desktop.
Remove Element
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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