Java Solution | O(n) | Clean solution


  • 0
    S
    public class Solution {
        public int removeElement(int[] nums, int val) {
            int i=0,j=0;
            while(j <nums.length){
                if(nums[j]==val){
                    while(j<nums.length && nums[j]==val){
                        j++;
                    }
                    if(j<nums.length)nums[i++]=nums[j++];
                }
                else{
                    nums[i++]=nums[j++];
                }
            }
            return i;
        }
    }
    

Log in to reply
 

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