Delete an Element from a Given Position in an Array
Last Updated :
08 Nov, 2024
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]
[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 splice() in JavaScript.
C++
// C++ program to delete an element from a given
// position in an array
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 30, 40 };
int pos = 2;
cout << "Array before deletion\n";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
// Delete the element at the given position
arr.erase(arr.begin() + pos - 1);
cout << "\nArray after deletion\n";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to delete an element from a given
// position of an array
import java.util.*;
class GfG {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>
(Arrays.asList(10, 20, 30, 40));
int pos = 2;
System.out.println("Array before deletion");
for (int i = 0; i < arr.size(); i++)
System.out.print(arr.get(i) + " ");
// Delete the element at the specified position
arr.remove(pos - 1);
System.out.println("\nArray after deletion");
for (int i = 0; i < arr.size(); i++)
System.out.print(arr.get(i) + " ");
}
}
Python
# Python program to delete an element from a given
# position of an array
arr = [10, 20, 30, 40]
pos = 2
print("Array before deletion")
for num in arr:
print(num, end=" ")
# Delete the element at the specified position
del arr[pos - 1]
print("\nArray after deletion")
for num in arr:
print(num, end=" ")
C#
// C# program to delete an element from a given
// position of an array
using System;
using System.Collections.Generic;
class GfG {
static void Main() {
List<int> arr = new List<int> { 10, 20, 30, 40 };
int pos = 2;
Console.WriteLine("Array before deletion");
foreach (int num in arr)
Console.Write(num + " ");
// Delete the element at the specified position
arr.RemoveAt(pos - 1);
Console.WriteLine("\nArray after deletion");
foreach (int num in arr)
Console.Write(num + " ");
}
}
JavaScript
// JavaScript program to delete an element from a given
// position of an array
let arr = [10, 20, 30, 40];
let pos = 2;
console.log("Array before deletion");
console.log(arr)
// Delete the element at the specified position
arr.splice(pos - 1, 1);
console.log("Array after deletion");
console.log(arr);
OutputArray 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)
[Approach 2] Using Custom Method
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.
C++
// C++ program to delete an element from a given
// position in an array using custom method
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 30, 40 };
int n = arr.size();
int pos = 2;
cout << "Array before deletion\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
// Delete the element at the given position
for(int i = pos; i < n; i++) {
arr[i - 1] = arr[i];
}
if(pos <= n)
n--;
cout << "\nArray after deletion\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
C
// C program to delete an element from a given
// position in an array using custom method
#include <stdio.h>
int main() {
int arr[] = { 10, 20, 30, 40 };
int n = sizeof(arr)/sizeof(arr[0]);
int pos = 2;
printf("Array before deletion\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
// Delete the element at the given position
for (int i = pos; i < n; i++) {
arr[i - 1] = arr[i];
}
if (pos <= n)
n--;
printf("\nArray after deletion\n");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
Java
// Java program to delete an element from a given
// position in an array using custom method
class GfG {
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40 };
int n = arr.length;
int pos = 2;
System.out.println("Array before deletion");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
// Delete the element at the given position
for (int i = pos; i < n; i++) {
arr[i - 1] = arr[i];
}
if (pos <= n) {
n--;
}
System.out.println("\nArray after deletion");
for (int i = 0; i < n; i++) {
System.out.print(arr[i] + " ");
}
}
}
Python
# Python program to delete an element from a given
# position in an array using custom method
if __name__ == "__main__":
arr = [10, 20, 30, 40]
n = len(arr)
pos = 2
print("Array before deletion")
for i in range(n):
print(arr[i], end=" ")
# Delete the element at the given position
for i in range(pos, n):
arr[i - 1] = arr[i]
if pos <= n:
n -= 1
print("\nArray after deletion")
for i in range(n):
print(arr[i], end=" ")
C#
// C# program to delete an element from a given
// position in an array using custom method
using System;
class GfG {
static void Main() {
int[] arr = { 10, 20, 30, 40 };
int n = arr.Length;
int pos = 2;
Console.WriteLine("Array before deletion");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
// Delete the element at the given position
for (int i = pos; i < n; i++) {
arr[i - 1] = arr[i];
}
if (pos <= n)
n--;
Console.WriteLine("\nArray after deletion");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
JavaScript
// JavaScript program to delete an element from a given
// position in an array using custom method
let arr = [ 10, 20, 30, 40 ];
let pos = 2;
let n = arr.length;
console.log("Array before deletion");
for (let i = 0; i < n; i++) {
console.log(arr[i]);
}
// Delete the element at the given position
for (let i = pos; i < n; i++) {
arr[i - 1] = arr[i];
}
if (pos <= n) {
n--;
}
console.log("\nArray after deletion");
for (let i = 0; i < n; i++) {
console.log(arr[i]);
}
OutputArray 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)
Similar Reads
Delete an Element from the end of an array 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: []Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Method[Approach 1] Using Built-In MethodsWe wil
5 min read
Delete First Occurrence of Given Element from an Array Given an array of integers, the task is to delete a given element from the array. If there are multiple occurrences of the element, we need to remove only its first occurrence.Examples:Input: arr[] = [10, 20, 30, 40], ele = 20Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], ele = 25Output: [10,
8 min read
Delete an Element from the Beginning of an Array 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: []Table of Content[Approach 1] Using Built-In Methods[Approach 2] Using Custom Methods[Approach 1] Using Built-In Method
6 min read
Deleting Elements in an Array - Array Operations In this post, we will look into deletion operation in an Array, i.e., how to delete an element from an Array, such as:Delete an Element from the Beginning of an ArrayDelete an Element from a Given Position in an ArrayDelete First Occurrence of Given Element from an ArrayRemove All Occurrences of an
4 min read
Delete array element in given index range [L - R] Given an array A[] and the size of an array is N. The task is to delete elements of array A[] that are in the given range L to R both are exclusive. Examples: Input : N = 12 A[] = { 3, 5, 3, 4, 9, 3, 1, 6, 3, 11, 12, 3} L = 2 R = 7 Output : 3 5 3 6 3 11 12 3 since A[2] = 3 but this is exclude A[7] =
10 min read