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, every element is checked. The space complexity of the function is O(1), as it uses a constant amount of extra space. The only additional variable used is `i`, which is an integer that keeps track of the position to write the next valid element. The function modifies the input list in place without requiring any additional data structures that 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
.