C# - overwrite elements to remove with val at current length


  • 0
    public int RemoveElement(int[] nums, int val) 
    {
        int len = nums.Length;
        int index = 0;
        while (index < len)
        {
            if (nums[index] == val)
            {
                nums[index] = nums[len-1];
                len--;
            }
            else
            {
                index++;        
            }
        }
        
        return len;
    }

Log in to reply
 

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