leetcode-study

27. Remove Element

class Solution:
    def removeElement(self, nums: List[int], val: int) -> int:
        """
        Remove all occurrences of 'val' from the list 'nums' in-place.
        
        Parameters:
            nums (List[int]): The list of integers to process.
            val (int): The integer value to remove from the list.
        
        Returns:
            int: The new length of the list 'nums' after removal, where the first k elements
                 contain the values that are not equal to 'val'.
        """
        write_index = 0  # This pointer tracks where the next non-'val' element should be written.
        
        # Iterate through each element in 'nums'
        for num in nums:
            # If the current element does not equal 'val', write it to the current write_index.
            if num != val:
                nums[write_index] = num  # Place the valid element at the 'write_index'
                write_index += 1         # Increment the write_index to the next insertion point
                
        # 'write_index' now represents the count of elements not equal to 'val'
        return write_index

Summary of Techniques and Approaches: