C# Solution


  • 0

    Two pointers, O(n), No extra space

        public int RemoveElement(int[] nums, int val) {
            if (nums == null || nums.Length == 0)
                return 0;
            
            int slowIndex = 0, fastIndex = 0;
            
            while (slowIndex <= nums.Length - 1 && fastIndex <= nums.Length - 1)
            {
                if (nums[fastIndex] != val)
                {
                    nums[slowIndex] = nums[fastIndex];
                    slowIndex++;
                    fastIndex++;
                }
                else
                    fastIndex++;
            }
            
            return slowIndex;
        }

Log in to reply
 

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