Delete First Occurrence of Given Element from an Array
Last Updated :
09 Nov, 2024
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 = 20
Output: [10, 30, 40]
Input: arr[] = [10, 20, 30, 40], ele = 25
Output: [10, 20, 30, 40]
Input: arr[] = [10, 20, 20, 20 30], ele = 20
Output: [10, 20, 20, 30]
[Approach 1] Using Built-In Methods
We will use library methods like erase() in C++, remove() in Java and Python, Remove() in C# and splice() in JavaScript.
C++
// C++ program to delete the first occurrence of an
// element in the array using built-in methods
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 20, 20, 30 };
int ele = 20;
cout << "Array before deletion\n";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
// Find the element in the array
auto it = find(arr.begin(), arr.end(), ele);
// Remove the element if it is present in array
if (it != arr.end()) {
arr.erase(it);
}
cout << "\nArray after deletion\n";
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
Java
// Java program to delete the first occurrence of an
// element in the array using built-in methods
import java.util.*;
class GfG {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<Integer>
(Arrays.asList(10, 20, 20, 20, 30));
int ele = 20;
System.out.println("Array before deletion");
for (int i = 0; i < arr.size(); i++)
System.out.print(arr.get(i) + " ");
// Remove the element from the array
arr.remove(Integer.valueOf(ele));
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 the first occurrence of an
# element in the array using built-in methods
if __name__ == "__main__":
arr = [10, 20, 20, 20, 30]
ele = 20
print("Array before deletion")
for num in arr:
print(num, end=" ")
# Remove the element if it is present in array
if ele in arr:
arr.remove(ele)
print("\nArray after deletion")
for num in arr:
print(num, end=" ")
C#
// C# program to delete the first occurrence of an
// element in the array using built-in methods
using System;
using System.Collections.Generic;
class GfG {
static void Main() {
List<int> arr = new List<int> { 10, 20, 20, 20, 30 };
int ele = 20;
Console.WriteLine("Array before deletion");
foreach (int num in arr)
Console.Write(num + " ");
arr.Remove(ele);
Console.WriteLine("\nArray after deletion");
foreach (int num in arr)
Console.Write(num + " ");
}
}
JavaScript
// JavaScript program to delete the first occurrence of
// an element in the array using built-in methods
let arr = [10, 20, 20, 20, 30];
let ele = 20;
console.log("Array before deletion");
console.log(arr)
// Find the element in the array
let idx = arr.indexOf(ele);
// Remove the element if it is present in array
if (idx !== -1) {
arr.splice(idx, 1);
}
console.log("Array after deletion");
console.log(arr)
OutputArray before deletion
10 20 20 20 30
Array after deletion
10 20 20 30
Time Complexity: O(n), where n is the size of input array.
Auxiliary Space: O(1)
[Approach 2] Using Custom Methods
The idea is to iterate over all the elements of the array and as soon as we encounter the element to be deleted, shift all the elements occurring to the right of the element, one position to the left.
C++
// C++ program to delete the first occurrence of an
// element in the array using custom method
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> arr = { 10, 20, 20, 20, 30 };
int n = arr.size();
int ele = 20;
cout << "Array before deletion\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
bool found = false;
for(int i = 0; i < n; i++) {
// If the element has been found previously,
// shift the current element to the left
if(found) {
arr[i - 1] = arr[i];
}
// check if the current element is equal to
// the element to be removed
else if(arr[i] == ele) {
found = true;
}
}
// If element was found, reduce the size of array
if(found == true)
n--;
cout << "\nArray after deletion\n";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
C
// C program to delete the first occurrence of an
// element in the array using custom method
#include <stdio.h>
#include <stdbool.h>
int main() {
int arr[] = { 10, 20, 20, 20, 30 };
int n = sizeof(arr)/sizeof(arr[0]);
int ele = 20;
printf("Array before deletion\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
bool found = false;
for (int i = 0; i < n; i++) {
// If the element has been found previously,
// shift the current element to the left
if (found) {
arr[i - 1] = arr[i];
}
// check if the current element is equal to
// the element to be removed
else if (arr[i] == ele) {
found = true;
}
}
// If element was found, reduce the size of array
if (found)
n--;
printf("\nArray after deletion\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java program to delete the first occurrence of an
// element in the array using custom method
class GfG {
public static void main(String[] args) {
int[] arr = { 10, 20, 20, 20, 30 };
int n = arr.length;
int ele = 20;
System.out.println("Array before deletion");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
boolean found = false;
for (int i = 0; i < n; i++) {
// If the element has been found previously,
// shift the current element to the left
if (found) {
arr[i - 1] = arr[i];
}
// check if the current element is equal to
// the element to be removed
else if (arr[i] == ele) {
found = true;
}
}
// If element was found, reduce the size of array
if (found == true)
n--;
System.out.println("\nArray after deletion");
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
}
Python
# Python program to delete the first occurrence of an
# element in the array using custom method
if __name__ == "__main__":
arr = [10, 20, 20, 20, 30]
n = len(arr)
ele = 20
print("Array before deletion")
for i in range(n):
print(arr[i], end=" ")
found = False
for i in range(n):
# If the element has been found previously,
# shift the current element to the left
if found:
arr[i - 1] = arr[i]
# check if the current element is equal to
# the element to be removed
elif arr[i] == ele:
found = True
# If element was found, reduce the size of array
if found:
n -= 1
print("\nArray after deletion")
for i in range(n):
print(arr[i], end=" ")
C#
// C# program to delete the first occurrence of an
// element in the array using custom method
using System;
class GfG {
static void Main() {
int[] arr = { 10, 20, 20, 20, 30 };
int n = arr.Length;
int ele = 20;
Console.WriteLine("Array before deletion");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
bool found = false;
for (int i = 0; i < n; i++) {
// If the element has been found previously,
// shift the current element to the left
if (found) {
arr[i - 1] = arr[i];
}
// check if the current element is equal to
// the element to be removed
else if (arr[i] == ele) {
found = true;
}
}
// If element was found, reduce the size of array
if (found) {
n--;
}
Console.WriteLine("\nArray after deletion");
for (int i = 0; i < n; i++) {
Console.Write(arr[i] + " ");
}
}
}
JavaScript
// JavaScript program to delete the first occurrence of an
// element in the array using custom method
let arr = [ 10, 20, 20, 20, 30 ];
let ele = 20;
let n = arr.length;
console.log("Array before deletion");
for(let i = 0; i < n; i++)
console.log(arr[i]);
let found = false;
for (let i = 0; i < n; i++) {
// If the element has been found previously,
// shift the current element to the left
if (found) {
arr[i - 1] = arr[i];
}
// check if the current element is equal to
// the element to be removed
else if (arr[i] === ele) {
found = true;
}
}
// If element was found, reduce the size of array
if (found === true) {
n--;
}
console.log("\nArray after deletion");
for (let i = 0; i < n; i++) {
console.log(arr[i] + " ");
}
OutputArray before deletion
10 20 20 20 30
Array after deletion
10 20 20 30
Time Complexity: O(n), where n is the size of input array.
Auxiliary Space: O(1)
Similar Reads
Delete first occurrence of given Key from a Linked List Given a linked list and a key to be deleted. The task is to delete the first occurrence of the given key from the linked list. Note: The list may have duplicates. Examples: Input: list = 1->2->3->5->2->10, key = 2Output: 1->3->5->2->10Explanation: There are two instances o
10 min read
First element occurring k times in an array Given an array arr[] of size n, the task is to find the first element that occurs k times. If no element occurs k times, print -1. If multiple elements occur k times, the first one in the array should be the answer. Examples: Input: arr = [1,7,4,3,4,8,7], k=2Output: 7Explanation: Both 7 and 4 occur
9 min read
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
Group multiple occurrence of array elements ordered by first occurrence Given an unsorted array with repetitions, the task is to group multiple occurrence of individual elements. The grouping should happen in a way that the order of first occurrences of all elements is maintained.Examples: Input: arr[] = {5, 3, 5, 1, 3, 3}Output: {5, 5, 3, 3, 3, 1}Input: arr[] = {4, 6,
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