juniorArrays

How do you delete an element from an array?

Updated Apr 28, 2026

Short answer

Deleting an element from an array means removing the value at a specific index and adjusting the remaining elements so the array stays valid. In most array implementations, this requires shifting all elements after the deleted position one place to the left, making deletion an O(n) operation in the general case. Some languages provide built-in methods like splice() or remove(), but they usually perform the same underlying shifting work.

Deep explanation

Arrays store elements in contiguous memory locations, which allows fast access by index. For example, if an array contains:

TEXT
Index: 0 1 2 3 4
Value: 10 20 30 40 50

If we delete the element at index 2, the value 30 is removed:

TEXT
Index: 0 1 2 3
Value: 10 20 40 50

The elements after the deleted item must shift left to fill the empty space.

Manual deletion process

The typical steps are:

  1. Identify the index of the element to delete.
  2. Shift every element after that index one position to the left.
  3. Reduce the logical size of the array by one.

Example in a language-agnostic style:

TEXT
for i from deleteIndex to arrayLength - 2:
array[i] = array[i + 1]
arrayLength = arrayLength - 1

The last element is now considered outside the active portion of the array.

Example implementation in Java

Java
public class ArrayDelete {
public static int[] deleteElement(int[] arr, int index) {
int[] result = new int[arr.length - 1];
for (int i = 0, j = 0; i < arr.length; i++) {
if (i != index) {
result[j++] = arr[i];
}
}
return result;
}
public static void main(String[] args) {
int[] numbers = {10, 20, 30, 40, 50};
int[] updated = deleteElement(numbers, 2);
for (int num : updated) {
System.out.print(num + " ");
}
}
}

Output:

TEXT
10 20 40 50

Time complexity

The performance depends on where the deletion happens:

  • Deleting from the end: O(1) in a dynamic array because no shifting is needed.
  • Deleting from the beginning: O(n) because every remaining element must move.
  • Deleting from the middle: O(n) because elements after the deleted index must shift.

Using built-in methods

Many programming languages provide simpler ways to delete elements.

For example, in

JavaScript
let numbers = [10, 20, 30, 40, 50];
numbers.splice(2, 1);
console.log(numbers);

Output:

TEXT
[10, 20, 40, 50]

Here:

  • 2 is the starting index.
  • 1 is the number of elements to remove.

Alternative approach: swapping with the last element

If the order of elements does not matter, deletion can be optimized.

Example:

TEXT
Original array:
[10, 20, 30, 40, 50]
Delete 30:
Replace 30 with 50 and reduce size.
Result:
[10, 20, 50, 40]

This avoids shifting and takes O(1) time, but it changes the array order.

This approach is useful in cases such as implementing sets, caches, or game object lists where ordering is not important.

Real-world example

Imagine a shopping cart application where a user removes an item. The application stores cart items in an array:

JavaScript
let cart = [
"Laptop",
"Keyboard",
"Mouse",
"Monitor"
];
// Remove "Mouse"
cart.splice(2, 1);
console.log(cart);

Output:

TEXT
["Laptop", "Keyboard", "Monitor"]

The item at index 2 is removed, and the remaining items shift left so the array remains continuous.

In a real application, developers often use higher-level data structures instead of manually managing arrays, but understanding the underlying deletion process helps explain performance behavior.

Common mistakes

  • * Deleting an element without shifting remaining elements, which leaves gaps or incorrect data.
  • * Forgetting to update the array size after manually removing an element.
  • * Assuming deletion from any position in an array is always `O(1)`.
  • * Using index-based deletion without checking whether the index is valid.
  • * Modifying an array while iterating over it and accidentally skipping elements.
  • * Choosing an array for frequent deletions from the middle when a linked list or another data structure may be more appropriate.

Follow-up questions

  • What is the time complexity of deleting an element from an array?
  • How would you delete an element from an array without preserving order?
  • What is the difference between deleting an element and setting it to a default value?
  • Why are deletions expensive in normal arrays compared to linked lists?
  • How would you remove all occurrences of a value from an array?
  • What happens if you delete an element while looping through an array?

More Arrays interview questions

View all →