> For the complete documentation index, see [llms.txt](https://tanias-workspace.gitbook.io/tanias-little-corner/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tanias-workspace.gitbook.io/tanias-little-corner/problems/remove-element.md).

# Remove Element

<figure><img src="/files/a0KnqmqLJ1S4beLY6Niq" alt=""><figcaption></figcaption></figure>

## Intuition

The key idea behind this solution is to efficiently remove all instances of a target value from an array by using two pointers: `index` and `i`. The `index` pointer tracks the position where the next non-target element should be placed, while the `i` pointer scans through the array. By overwriting target values with non-target elements as they are encountered, the solution effectively removes all occurrences of the target value in-place.

## Approach

1. **Initialize Pointers**: Start by setting `index` to 0, indicating the position for the next non-target element.
2. **Iterate Through the Array**: Use the `i` pointer to iterate through each element of the array.
3. **Check Elements**: For each element `nums[i]`, determine if it is equal to the target value.
4. **Store Non-Target Elements**: If `nums[i]` is not the target value, assign `nums[index] = nums[i]` to store the non-target element at the current `index` position.
5. **Advance the Index Pointer**: Increment `index` by 1 to prepare for the next non-target element.
6. **Repeat**: Continue this process until all elements have been processed.
7. **Return the Result**: The final value of `index` will represent the length of the array after all target values have been removed.

<figure><img src="/files/HBbV93IZRJLA0YTNSHys" alt=""><figcaption></figcaption></figure>

#### Complexity

* **Time Complexity**: O(n), where `n` is the number of elements in the array. The solution only requires a single pass through the array.
* **Space Complexity**: O(1), as the solution operates in-place and does not require any additional memory beyond the input array.
