Very simple solution in O(n)


  • 0
    T
    public class Solution {
        public int removeElement(int[] nums, int val) {
            int i = -1;
            if(nums.length == 0) return 0;
            int j = 0;
            while(j<nums.length){
                if(nums[j]!=val){
                    nums[++i] = nums[j];
                }
                j++;
            }
            return i+1;
        }
    }

Log in to reply
 

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