Skip to content

Latest commit

 

History

History
32 lines (25 loc) · 569 Bytes

Remove Element.md

File metadata and controls

32 lines (25 loc) · 569 Bytes

Notes

The problem mentions "It doesn't matter what you leave beyond the new length."

Solution

class Solution {
    public int removeElement(int[] nums, int val) {
        if (nums == null) {
            return 0;
        }
        int i = 0;
        for (int n : nums) {
            if (n != val) {
                nums[i] = n;
                i++;
            }
        }
        return i;
    }
}

Time/Space Complexity

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Links