Share my JAVA solution

  • 1

    I use two indexes to locate the element in the array. The index "first" is to locate the position which different element should be inserted in. The index "second" is to identify the current position in the array. If program encounter the element that it want to remove, it just reduce the length and jump to next element in the array.

    public class Solution {
        public int removeElement(int[] A, int elem) {
            if(A.length == 0) return 0;
            int first = 0, second = 0;
            int length = A.length;
            while(second < A.length){
                if(A[second] == elem){
                } else {
                    A[first] = A[second];
            return length;

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.