array manipulation
data structure
remove element
programming tutorial
array operations

Remove element of a regular array

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

Removing an element from an array sounds simple, but the exact answer depends on what “regular array” means in the language you are using. Some languages have fixed-size arrays, where removal really means building a new array or shifting values manually, while others expose dynamic array-like containers that hide most of that work for you.

Arrays Are Contiguous Storage

The reason removal is not constant-time in the general case is that arrays store elements contiguously. If you remove one element from the middle, everything after it usually has to move left by one position to fill the gap.

That means the usual time cost is O(n) for removal at an arbitrary position.

Dynamic Arrays Make It Look Easy

In languages with built-in dynamic arrays, the runtime performs the shift for you.

Python example:

python
values = [10, 20, 30, 40]
del values[1]
print(values)

JavaScript example:

javascript
const values = [10, 20, 30, 40];
values.splice(1, 1);
console.log(values);

These APIs are convenient, but the underlying work still exists. The array contents are being compacted after removal.

Fixed-Size Arrays Need Manual Strategy

In lower-level languages such as C, arrays do not resize themselves. If you want to remove an element while keeping order, you shift later elements left and track the new logical length separately.

c
1#include <stdio.h>
2
3int remove_at(int arr[], int length, int index) {
4    if (index < 0 || index >= length) {
5        return length;
6    }
7
8    for (int i = index; i < length - 1; i++) {
9        arr[i] = arr[i + 1];
10    }
11
12    return length - 1;
13}
14
15int main(void) {
16    int arr[] = {10, 20, 30, 40};
17    int length = 4;
18
19    length = remove_at(arr, length, 1);
20
21    for (int i = 0; i < length; i++) {
22        printf("%d ", arr[i]);
23    }
24    printf("\n");
25    return 0;
26}

The physical array still exists at the original size, but your code now treats only the first length - 1 elements as valid.

Removing by Value Is a Two-Step Problem

Sometimes you know the value you want to remove, not its index. In that case, you first search for the value and then remove it.

python
1def remove_first(values, target):
2    try:
3        values.remove(target)
4    except ValueError:
5        pass
6    return values
7
8print(remove_first([1, 2, 3, 2], 2))

This removes the first matching occurrence. If duplicates matter, you need to define whether to remove one element or all of them.

Preserving Order Versus Fast Removal

If order does not matter, you can sometimes remove an element faster by swapping it with the last element and then shrinking the logical size.

That is common in performance-sensitive code where array order is irrelevant. If order matters, though, shifting remains necessary.

So the real design question is often not just “how do I remove it,” but “must the original ordering survive.”

A New Array Can Be Cleaner

In immutable or functional-style code, instead of mutating the original array you may create a new one without the unwanted element.

python
values = [10, 20, 30, 40]
new_values = values[:1] + values[2:]
print(new_values)

This is often clearer when mutation is undesirable, even though it allocates new storage.

Common Pitfalls

The most common mistake is assuming removal from the middle of an array is constant-time. It usually is not.

Another common issue is forgetting bounds checks when removing by index. Developers also often confuse fixed-size arrays with dynamic list-like containers and then expect resizing behavior that the underlying structure does not provide.

Summary

  • Removing from an array usually requires shifting later elements left.
  • Dynamic arrays hide the shifting, but the cost is still typically O(n).
  • Fixed-size arrays need manual shifting and a new logical length.
  • Removing by value means finding the element first.
  • If order does not matter, faster swap-with-last strategies may be possible.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.