![]() |
VOOZH | about |
Given an array of integers, the task is to delete an element from a given position in the array.
Examples:
Input: arr[] = [10, 20, 30, 40], pos = 1
Output: [20, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 2
Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 4
Output: [10, 20, 30]
We will use library methods like erase() in C++, remove() in Java, del in Python, removeAt() in C# and splice() in JavaScript.
Array before deletion 10 20 30 40 Array after deletion 10 30 40
Time Complexity: O(n), where n is the size of array.
Auxiliary Space: O(1)
The idea is to shift all the elements occurring after the given position, one index to the left and reduce the size of the array by 1.
Array before deletion 10 20 30 40 Array after deletion 10 30 40
Time Complexity: O(n), where n is the size of array.
Auxiliary Space: O(1)