Delete an Element from the end of an array (original) (raw)
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: []
Table of Content
[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 #include using namespace std;
int main() { vector 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 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 arr = new List { 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);
`
Output
Array 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 #include using namespace std;
int main() { vector 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]); }
`
Output
Array 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 all odd frequency elements from an Array Given an array arr containing integers of size N, the task is to delete all the elements from the array that have odd frequencies.Examples: Input: arr[] = {3, 3, 3, 2, 2, 4, 7, 7} Output: {2, 2, 7, 7} Explanation: Frequency of 3 = 3 Frequency of 2 = 2 Frequency of 4 = 1 Frequency of 7 = 2 Therefore, 5 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
- Minimum delete operations to make all elements of array same Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.Examples: Input: arr[] = {4, 3, 4, 4, 2, 4} Output: 2 After deleting 2 and 3 from array, arr 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
- Find last element of Array by rotating and deleting N-K+1 element Given an array arr[] of N integers, the task is to find the element which is left at last after performing the following operation N - 1 time. For every Kth operation: Right-rotate the array clockwise by 1.Delete the (n - K + 1)th last element. Example: Input: N = 6, arr[] = {1, 2, 3, 4, 5, 6}Output 7 min read
- Find start and ending index of an element in an unsorted array Given an array of integers, task is to find the starting and ending position of a given key. Examples: Input : arr[] = {1, 2, 3, 4, 5, 5} Key = 5Output : Start index: 4 Last index: 5Explanation: Starting index where 5is present is 4 and ending index is 5. Input :arr[] = {1, 3, 7, 8, 6}, Key = 2Outpu 11 min read