7line Java solution with 2 pointer variables to the array

  • 3
    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) 
                //we can also use length as index to cover val...
            	nums[length] = nums[i];
            return length;

Log in to reply

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