Avoid copying if no equal number.


  • 0
    L
    public int removeElement(int[] nums, int val) {
            int index = 0;
            boolean shifted = false;
            for (int n : nums) {
                if (val == n) shifted = true;
                else if (shifted) nums[index++] = n;
                else index++;
            }
            return index;
        }
    

Log in to reply
 

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