Delete an Element from the end of an array Last Updated : 08 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 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 MethodTo 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) Comment More infoAdvertise with us Next Article Delete an Element from the end of an array M mrityuanjay8vae Follow Improve Article Tags : DSA Arrays Arrays Practice Tags : ArraysArrays 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 Remove All Occurrences of an Element in an Array Given an integer array arr[] and an integer ele the task is to the remove all occurrences of ele from arr[] in-place and return the number of elements which are not equal to ele. If there are k number of elements which are not equal to ele then the input array arr[] should be modified such that the 6 min read Search, Insert, and Delete in an Unsorted Array | Array Operations In this post, a program to search, insert, and delete operations in an unsorted array is discussed.Search Operation:In an unsorted array, the search operation can be performed by linear traversal from the first element to the last element. Coding implementation of the search operation:C++// C++ prog 15+ min read Find the position of the last removed element from the array Given an array of size N and an integer M . Perform the following operations on the given array: If a[i] > M then push a[i] - M to end of the array, otherwise remove it from the array.Perform the first operation while the array is non-empty. The task is to find the original position of the elemen 6 min read Find the Array element left after alternate removal of minimum and maximum Given an array Arr of length N, it is reduced by 1 element at each step. Maximum and Minimum elements will be removed in alternating order from the remaining array until a single element is remaining in the array. The task is to find the remaining element in the given array. Examples: Input: arr[] = 4 min read Minimum elements to be removed from the ends to make the array sorted Given an array arr[] of length N, the task is to remove the minimum number of elements from the ends of the array to make the array non-decreasing. Elements can only be removed from the left or the right end. Examples: Input: arr[] = {1, 2, 4, 1, 5} Output: 2 We can't make the array sorted after one 9 min read Like