Description
Given an array and a value, remove all occurrences of that value in place and return the new length.
The order of elements can changed, and the elements after the new length don ' t matter.
Example
Given an array [0,4,4,0,0,2,4,4]
,value=4
Return and front four elements of the 4
array is[0,0,0,2]
Problem solving: Today the topic is quite simple, given an array, and then given a value, requires the array in the existence of this value, are eliminated. On the basis of the original array, a rear is set as the subscript for the position to be inserted, starting from 0. Iterate over the array, if it is not value, place it in the rear position, and if so, do nothing until the loop ends. Finally, the rear value is returned, that is, the number of elements to remove value. The code is as follows:
1 Public classSolution {2 /*3 * @param a:a list of integers4 * @param elem:an integer5 * @return: The new length after remove6 */7 Public intRemoveelement (int[] A,intelem) {8 //Write your code here9 intRear = 0;Ten for(inti = 0; i < a.length; i++){ One if(A[i]! =elem) { Aa[rear++] =A[i]; - } - } the returnRear; - } -}
172. Remove Element "Lintcode by Java"