Bird
Raised Fist0
DSA Javaprogramming

Array Deletion at End in DSA Java

Choose your learning style10 modes available
Mental Model
Removing the last item from a list means just ignoring it from now on.
Analogy: Imagine a stack of plates where you always take the top plate off first. Deleting at the end is like removing the top plate from the stack.
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [5]
Length: 5 ↑
Dry Run Walkthrough
Input: array: [1, 2, 3, 4, 5], delete last element
Goal: Remove the last element and reduce the array size by one
Step 1: Check if array is empty
Index: 0   1   2   3   4
Array: [1] [2] [3] [4] [5]
Length: 5 ↑
Why: We must ensure there is something to delete
Step 2: Reduce the length by one to ignore the last element
Index: 0   1   2   3
Array: [1] [2] [3] [4] [5]
Length: 4 ↑
Why: By reducing length, the last element is no longer considered part of the array
Step 3: Return the new array state
Index: 0   1   2   3
Array: [1] [2] [3] [4]
Length: 4 ↑
Why: The array now effectively ends at the fourth element
Result:
Index: 0   1   2   3
Array: [1] [2] [3] [4]
Length: 4
Annotated Code
DSA Java
public class ArrayDeletionAtEnd {
    static class Array {
        int[] data;
        int length;

        Array(int size) {
            data = new int[size];
            length = 0;
        }

        void add(int value) {
            if (length < data.length) {
                data[length] = value;
                length++;
            }
        }

        boolean deleteAtEnd() {
            if (length == 0) {
                return false; // nothing to delete
            }
            length--; // reduce length to ignore last element
            return true;
        }

        void print() {
            for (int i = 0; i < length; i++) {
                System.out.print(data[i]);
                if (i < length - 1) System.out.print(" -> ");
            }
            System.out.println(" -> null");
        }
    }

    public static void main(String[] args) {
        Array arr = new Array(5);
        arr.add(1);
        arr.add(2);
        arr.add(3);
        arr.add(4);
        arr.add(5);

        arr.print();
        arr.deleteAtEnd();
        arr.print();
    }
}
if (length == 0) {
check if array is empty to avoid deleting from empty array
length--; // reduce length to ignore last element
reduce length by one to delete last element logically
for (int i = 0; i < length; i++) {
print elements up to current length to show current array state
OutputSuccess
1 -> 2 -> 3 -> 4 -> 5 -> null 1 -> 2 -> 3 -> 4 -> null
Complexity Analysis
Time: O(1) because deleting at the end just reduces the length without shifting elements
Space: O(1) because no extra space is needed for deletion
vs Alternative: Compared to deleting at the start or middle which requires shifting elements O(n), deleting at end is faster and simpler
Edge Cases
empty array
deleteAtEnd returns false and array remains empty
DSA Java
if (length == 0) {
array with one element
deleting last element makes array empty with length zero
DSA Java
length--;
When to Use This Pattern
When you need to remove the last item from a list or stack quickly, use array deletion at end because it is simple and fast.
Common Mistakes
Mistake: Trying to shift elements after deleting at end
Fix: Just reduce the length by one; no shifting needed
Mistake: Not checking if array is empty before deleting
Fix: Add a check to avoid deleting when length is zero
Summary
Deletes the last element by reducing the array length by one.
Use when you want to remove the last item quickly without rearranging the array.
The key is that reducing length ignores the last element without moving data.

Practice

(1/5)
1. What happens when you delete an element from the end of a fixed-size array in Java?
easy
A. The last element is removed and the array size decreases by one
B. The last element is set to zero or null but the array size stays the same
C. All elements shift left by one position
D. The array is completely cleared

Solution

  1. Step 1: Understand fixed-size array behavior

    Java arrays have a fixed size once created, so their length cannot change.
  2. Step 2: Effect of deleting at end

    Deleting means logically ignoring the last element or setting it to default (0 or null), but the array length remains unchanged.
  3. Final Answer:

    The last element is set to zero or null but the array size stays the same -> Option B
  4. Quick Check:

    Fixed-size array length unchanged on deletion [OK]
Hint: Fixed arrays keep size; deletion just clears last element [OK]
Common Mistakes:
  • Thinking array size shrinks automatically
  • Assuming elements shift after deletion
  • Believing array clears fully on one deletion
2. Which of the following is the correct syntax to delete the last element from an array named arr in Java by reducing its logical size stored in size?
easy
A. arr[size--] = 0;
B. arr[size] = 0; size--;
C. size--; arr[size] = 0;
D. arr[size] = null; size++;

Solution

  1. Step 1: Understand logical deletion

    To delete last element, first reduce size by one, then clear that element.
  2. Step 2: Check syntax correctness

    size--; arr[size] = 0; reduces size first (size--) then sets arr[size] to 0, correctly deleting last element.
  3. Final Answer:

    size--; arr[size] = 0; -> Option C
  4. Quick Check:

    Decrease size before clearing element [OK]
Hint: Decrease size first, then clear element [OK]
Common Mistakes:
  • Clearing element before decreasing size
  • Using post-decrement incorrectly
  • Increasing size instead of decreasing
3. What will be the output of the following Java code?
int[] arr = {10, 20, 30, 40, 50};
int size = 5;
size--;
arr[size] = 0;
for(int i = 0; i < size; i++) {
    System.out.print(arr[i] + " ");
}
medium
A. 10 20 30 0 0
B. 10 20 30 40 0
C. 10 20 30 40 50
D. 10 20 30 40

Solution

  1. Step 1: Understand size decrement and element clearing

    Size is reduced from 5 to 4, then arr[4] is set to 0, removing last element logically.
  2. Step 2: Loop prints elements from index 0 to 3

    Loop runs i=0 to 3, printing arr[0]=10, arr[1]=20, arr[2]=30, arr[3]=40.
  3. Final Answer:

    10 20 30 40 -> Option D
  4. Quick Check:

    Last element removed, loop prints first 4 elements [OK]
Hint: Loop runs till size, last element skipped after decrement [OK]
Common Mistakes:
  • Printing all 5 elements ignoring size
  • Not updating last element to zero
  • Confusing array index with size
4. Identify the error in this Java code snippet that tries to delete the last element from an array:
int[] arr = {1, 2, 3, 4};
int size = 4;
arr[size] = 0;
size--;
medium
A. Array index out of bounds error at arr[size] = 0;
B. Size variable is not decremented
C. Array elements are not shifted left
D. No error, code works fine

Solution

  1. Step 1: Check array indexing

    Array indices go from 0 to size-1, so arr[size] is out of bounds.
  2. Step 2: Identify correct order

    Must decrement size first, then access arr[size] to avoid error.
  3. Final Answer:

    Array index out of bounds error at arr[size] = 0; -> Option A
  4. Quick Check:

    Accessing arr[size] causes error [OK]
Hint: Decrement size before accessing arr[size] [OK]
Common Mistakes:
  • Accessing arr[size] directly
  • Not decrementing size before deletion
  • Ignoring array index limits
5. You have an array int[] arr = {5, 10, 15, 20, 25} and a variable size = 5. You want to logically delete the last two elements without modifying the values in the array and print the remaining elements. Which code snippet correctly does this?
hard
A. size -= 2; for(int i = 0; i < size; i++) System.out.print(arr[i] + " ");
B. arr[size-1] = 0; arr[size-2] = 0; size -= 2; for(int i = 0; i < size; i++) System.out.print(arr[i] + " ");
C. size -= 2; arr[size] = 0; arr[size+1] = 0; for(int i = 0; i < size; i++) System.out.print(arr[i] + " ");
D. for(int i = 0; i < size-2; i++) System.out.print(arr[i] + " ");

Solution

  1. Step 1: Reduce logical size by 2

    Decreasing size by 2 means last two elements are ignored logically.
  2. Step 2: Print elements up to new size

    Loop from 0 to size-1 prints remaining elements correctly.
  3. Final Answer:

    size -= 2; for(int i = 0; i < size; i++) System.out.print(arr[i] + " "); -> Option A
  4. Quick Check:

    Logical size controls printed elements [OK]
Hint: Reduce size, then print up to size [OK]
Common Mistakes:
  • Setting elements to zero before reducing size
  • Printing without adjusting size
  • Accessing invalid indices after deletion
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard