🗑️ Array Deletion in Java – A Complete Beginner’s Guide (With Examples)

Arrays are one of the most fundamental data structures in programming. While accessing elements in an array is extremely fast, deleting an element requires some extra work.

In this blog, we’ll learn:

  • ✅ What is Array Deletion?
  • ✅ Why elements need to be shifted
  • ✅ Java program for array deletion
  • ✅ Step-by-step explanation
  • ✅ What happens after deletion?
  • ✅ Time Complexity of Array Deletion

Let’s dive in!


📌 What is Array Deletion?

Array deletion is the process of removing an element from an array.

Suppose we have the following array:

Index : 0   1   2   3   4

Array : 10  20  30  40  50

Now we want to delete the element 30 located at index 2.

After deletion, the array should become:

Index : 0   1   2   3

Array : 10  20  40  50

Notice that 40 and 50 have moved one position to the left.

This movement is called left shifting.


🤔 Why Do We Need to Shift Elements?

Arrays store their elements in contiguous (continuous) memory locations.

When an element is removed, it leaves an empty gap.

For example,

Before Deletion

10 20 30 40 50

Delete 30

10 20 __ 40 50

Arrays cannot have empty spaces between elements.

To remove this gap, every element after the deleted element must move one position to the left.

After shifting,

10 20 40 50

The gap disappears, and the array remains continuous.


📊 Visual Representation

Before Deletion

+----+----+----+----+----+
|10  |20  |30  |40  |50  |
+----+----+----+----+----+

Delete the element at index 2.

Step 1: Shift Elements to the Left

40 ← Move Left

50 ← Move Left

Intermediate array:

+----+----+----+----+----+
|10  |20  |40  |50  |50  |
+----+----+----+----+----+

Notice that the last value appears twice temporarily.

This is perfectly normal.


Step 2: Reduce the Logical Size

Logical Size = 4

Now the array is considered as:

+----+----+----+----+
|10  |20  |40  |50  |
+----+----+----+----+

Deletion is complete!


💻 Java Program for Array Deletion

public class ArrayDeletion {

    public static void main(String[] args) {

        int arr[] = {10, 20, 30, 40, 50};

        int size = 5;

        int position = 2;

        // Shift elements to the left
        for (int i = position; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }

        size--;

        // Display updated array
        for (int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output

10 20 40 50

🔍 Step-by-Step Execution

Initial Array

10 20 30 40 50

Current Size = 5

Delete element at index 2.


First Iteration

Move 40 to index 2.

10 20 40 40 50

Second Iteration

Move 50 to index 3.

10 20 40 50 50

Reduce the Size

size--;

New logical size becomes 4.

Final array:

10 20 40 50

🤔 Why Don’t We Remove the Last Element?

After shifting, the last element still exists in memory.

10 20 40 50 50

Does this mean deletion failed?

No.

The array still physically contains five positions because arrays have a fixed size.

However, after decreasing the logical size, we simply ignore the last element.

When printing the array, we print only up to the updated size.

for(int i = 0; i < size; i++)

Therefore, the output becomes

10 20 40 50

⚠️ Can We Reduce the Size of an Array?

The answer is No.

Once an array is created,

int arr[] = new int[5];

its size is permanently fixed.

Even after deleting elements, the array still contains five memory locations.

Deletion only decreases the logical size, not the actual memory allocated.


📊 Physical Size vs Logical Size

Suppose we create

int arr[] = new int[5];

Initially,

Physical Size = 5

Logical Size = 5

After deleting one element,

Physical Size = 5

Logical Size = 4

The array still occupies memory for five elements, but we now consider only the first four elements as valid.

This is an important concept in data structures.


💻 Example Showing Logical Size

public class LogicalSizeDemo {

    public static void main(String[] args) {

        int arr[] = {10, 20, 30, 40, 50};

        int size = 5;

        int position = 2;

        for(int i = position; i < size - 1; i++) {
            arr[i] = arr[i + 1];
        }

        size--;

        System.out.println("Logical Size = " + size);

        for(int i = 0; i < size; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

Output

Logical Size = 4

10 20 40 50

⏱️ Time Complexity of Array Deletion

The time taken depends on which element is deleted.

Deletion PositionTime Complexity
First ElementO(n)
Middle ElementO(n)
Last ElementO(1)

Why O(n)?

Suppose we delete the first element.

10 20 30 40 50

After deletion,

20 30 40 50

Every remaining element has to move one position to the left.

Since all elements may need to be shifted, the worst-case time complexity is O(n).


💡 Real-Life Analogy

Imagine five students standing in a line.

A  B  C  D  E

If student C leaves the line,

A  B  _  D  E

There is now an empty space.

To maintain the line, D and E move one step forward.

A  B  D  E

This is exactly how array deletion works.


📝 Key Takeaways

  • Array deletion means removing an element from a specific position.
  • Arrays cannot contain empty gaps between elements.
  • After deletion, all subsequent elements must shift one position to the left.
  • Arrays have a fixed physical size that cannot be reduced.
  • Deletion decreases the logical size of the array.
  • Deleting the last element requires no shifting and takes O(1) time.
  • Deleting the first or a middle element requires shifting and takes O(n) time.

🎯 Final Thoughts

Array deletion is a fundamental operation that every programmer should understand. Although arrays provide fast access to elements using indexes, deleting an element requires shifting the remaining elements to maintain continuous storage.

This is one of the reasons why arrays are excellent for fast access but not always the best choice when frequent insertions and deletions are required. Data structures like Linked Lists are designed to overcome this limitation and will be explored in future topics.

Now that you’ve mastered Array Traversal, Array Insertion, and Array Deletion, you’re well prepared to move on to Searching and Sorting Algorithms, which are essential building blocks of Data Structures and Algorithms.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *