Delete an Element from the end of an array
Last Updated :
08 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#.
C++
// C++ program to delete an element from the end
// of an array using in-built methods
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 30, 40 };
cout << "Array before deletion\n";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
// Remove the last element from the array
arr.pop_back();
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 the end of an array
// using in-built methods
import java.util.*;
class GfG {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>
(Arrays.asList(10, 20, 30, 40));
System.out.println("Array before deletion");
for (int ele : arr)
System.out.print(ele + " ");
// Remove the last element from the array
arr.remove(arr.size() - 1);
System.out.println("\nArray after deletion");
for (int ele : arr)
System.out.print(ele + " ");
}
}
Python
# Python program to delete an element from the end
# of an array using in-built methods
arr = [10, 20, 30, 40]
print("Array before deletion")
for ele in arr:
print(ele, end=" ")
# Remove the last element from the array
arr.pop()
print("\nArray after deletion")
for ele in arr:
print(ele, end=" ")
C#
// C# program to delete an element from the end
// of an array using in-built methods
using System;
using System.Collections.Generic;
class GfG {
static void Main() {
List<int> arr = new List<int> { 10, 20, 30, 40 };
Console.WriteLine("Array before deletion");
foreach (var ele in arr)
Console.Write(ele + " ");
// Remove the last element from the array
arr.RemoveAt(arr.Count - 1);
Console.WriteLine("\nArray after deletion");
foreach (var ele in arr)
Console.Write(ele + " ");
}
}
JavaScript
// JavaScript program to delete an element from the end
// of an array using in-built methods
let arr = [10, 20, 30, 40];
console.log("Array before deletion");
console.log(arr);
// Remove the last element from the array
arr.pop();
console.log("Array after deletion");
console.log(arr);
OutputArray 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.
C++
// C++ program to delete an element from the end
// of an array using custom methods
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 30, 40 };
int n = arr.size();
cout << "Array before deletion\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
// Reduce the array size by 1
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 the end
// of an array using custom methods
#include <stdio.h>
int main() {
int arr[] = { 10, 20, 30, 40 };
int n = sizeof(arr)/sizeof(arr[0]);
printf("Array before deletion\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
// Reduce the array size by 1
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 the end
// of an array using custom methods
class GfG {
public static void main(String[] args) {
int[] arr = { 10, 20, 30, 40 };
int n = arr.length;
System.out.println("Array before deletion");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
// Reduce the array size by 1
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 the end
# of an array using custom methods
if __name__ == "__main__":
arr = [10, 20, 30, 40]
n = len(arr)
print("Array before deletion")
for i in range(n):
print(arr[i], end=" ")
# Reduce the array size by 1
n -= 1
print("\nArray after deletion")
for i in range(n):
print(arr[i], end=" ")
C#
// C# program to delete an element from the end
// of an array using custom methods
using System;
class GfG {
static void Main() {
int[] arr = { 10, 20, 30, 40 };
int n = arr.Length;
Console.WriteLine("Array before deletion");
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
// Reduce the array size by 1
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 the end
// of an array using custom methods
let arr = [ 10, 20, 30, 40 ];
let n = arr.length;
console.log("Array before deletion");
for (let i = 0; i < n; i++) {
console.log(arr[i]);
}
// Reduce the array size by 1
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 20 30
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
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
Delete an Element from a Given Position in an Array 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 = 1Output: [20, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 2Output: [10, 30, 40]Input: arr[] = [10, 20, 30, 40], pos = 4Output: [10, 20, 30]Table of Conten
6 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
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