VOOZH about

URL: https://www.geeksforgeeks.org/dsa/delete-an-element-from-the-end-of-an-array/

⇱ Delete an Element from the end of an array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Delete an Element from the end of an array

Last Updated : 8 Nov, 2024

Given an array of integers, the task is to delete an element from the end of the array.

Examples:

Input: arr[] = [10, 20, 30, 40]
Output: [10, 20, 30]

Input: arr[] = [20]
Output: []

[Approach 1] Using Built-In Methods

We will use library methods like pop_back() in C++, remove() in Java, pop() in Python and JavaScript and removeAt() in C#.


Output
Array before deletion
10 20 30 40 
Array after deletion
10 20 30 

Time Complexity: O(1) 
Auxiliary Space: O(1)

[Approach 2] Using Custom Method

To delete an element from the end of an array, we can simply reduce the size of array by 1.


Output
Array before deletion
10 20 30 40 
Array after deletion
10 20 30 

Time Complexity: O(1) 
Auxiliary Space: O(1)


Comment
Article Tags:
Article Tags: