Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.
Search
Jun 27, 20241 min readThe time complexity of the `removeElement` function is O(n), where n is the number of elements in the input list `nums`. This is because the function iterates through the list once, checking each element to see if it is equal to the specified value `val`. In the worst case, all elements are processed. The space complexity is O(1), as the function modifies the input list in place and uses only a constant amount of additional space (the variable `i` for indexing). No extra data structures are used that would scale with the size of the input.
Given an integer array nums
and an integer val
, remove all occurrences of val
in nums
in-place. The order of the elements may be changed. Then return the number of elements in nums
which are not equal to val
.