7line Java solution with 2 pointer variables to the array


  • 3
    N
    public class Solution {
        public int removeElement(int[] nums, int val) {
            // length is the return array's length
            int length = 0;
            for(int i=0;i<nums.length;i++){
            	if(nums[i] == val) 
            		continue;
                //we can also use length as index to cover val...
            	nums[length] = nums[i];
            	length++;
            }
            return length;
        }
    }
    
    

Log in to reply
 

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