My easy java solution 1ms


  • 3
    N
    public int removeElement(int[] nums, int val) {
        int n=0;
        for(int i=0;i<nums.length;i++)
        {
          if(nums[i]!=val) nums[n++]=nums[i];
        }
        return (n);
    }

  • 0
    H
    public int removeElement(int[] nums, int val) {
        int index =0;
        for(int i=0;i<nums.length;i++){
            if(nums[i]!=val){
                if(i==index){
                    index++;
                }else{
                    nums[index++]=nums[i];
                }
            }
        }
        return index;
    }

Log in to reply
 

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