VOOZH about

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

⇱ Delete an Element from the Beginning of an Array - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Delete an Element from the Beginning of an Array

Last Updated : 8 Nov, 2024

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

Examples:

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

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

[Approach 1] Using Built-In Methods

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


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

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

[Approach 2] Using Custom Methods

The idea is to start from the second element and shift all the elements one position to the left. After shifting all the elements, reduce the array size by 1 to remove the extra element at the end.


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

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

Comment
Article Tags:
Article Tags: