Simple Java Solution


  • 0
    public int removeElement(int[] nums, int val) {
            int newLen=0;
            for(int j=0; j<nums.length; ++j)
            {
                if(nums[j]!=val)
                {
                    if(newLen!=j) nums[newLen++]=nums[j];
                    else newLen++; //newLen==j, no need to write
                }
            }
            return newLen;
        }
    

Log in to reply
 

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