Search⌘ K
AI Features

Remove Element

Understand how to use the two pointers technique to remove all instances of a given value from an array in place. Learn to adjust the array so the first portion contains the desired elements and return the count of these elements, mastering a key pattern for coding interviews.

Statement

You are given an integer array, nums, and an integer, val. Your task is to remove all occurrences of val from nums in place, meaning no additional memory allocation should be used. The relative order of the elements in the array may be changed. After modifying the array, return the number of elements that are not equal to val.

Let kk represent the number of elements in nums that are not equal to val. To successfully solve this problem, you need to ensure the following:

  • Modify the array, nums, such that the first kk ...