In this post, we will look into deletion operation in an Array, i.e., how to delete an element from an Array, such as:
- Deleting Elements from an Array when it is Unsorted
- Deleting Elements from an Array when it is Sorted
Deleting Elements in an Array when it is Unsorted
In the delete operation, the element to be deleted is searched using the linear search, and then the delete operation is performed followed by shifting the elements.

C++
<?php
function findElement(& $arr , $n , $key )
{
for ( $i = 0; $i < $n ; $i ++)
if ( $arr [ $i ] == $key )
return $i ;
return -1;
}
function deleteElement(& $arr , $n , $key )
{
$pos = findElement( $arr , $n , $key );
if ( $pos == -1)
{
echo "Element not found" ;
return $n ;
}
for ( $i = $pos ; $i < $n - 1; $i ++)
$arr [ $i ] = $arr [ $i + 1];
return $n - 1;
}
$arr = array (10, 50, 30, 40, 20);
$n = count ( $arr );
$key = 30;
echo "Array before deletion\n" ;
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
$n = deleteElement( $arr , $n , $key );
echo "\nArray after deletion\n" ;
for ( $i = 0; $i < $n ; $i ++)
echo $arr [ $i ] . " " ;
?>
|
Output
Array before deletion
10 50 30 40 20
Array after deletion
10 50 40 20
Time Complexity: O(N) Auxiliary Space: O(1)
Deleting Elements in an Array when it is Sorted
In the delete operation, the element to be deleted is searched using binary search, and then the delete operation is performed followed by shifting the elements.
 Performing delete operation
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int binarySearch( int arr[], int low, int high, int key);
int deleteElement( int arr[], int n, int key)
{
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
cout << "Element not found" ;
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch( int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 30;
cout << "Array before deletion\n" ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
n = deleteElement(arr, n, key);
cout << "\n\nArray after deletion\n" ;
for (i = 0; i < n; i++)
cout << arr[i] << " " ;
}
|
C
#include <stdio.h>
int binarySearch( int arr[], int low, int high, int key);
int deleteElement( int arr[], int n, int key)
{
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
printf ( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
int binarySearch( int arr[], int low, int high, int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
int main()
{
int i;
int arr[] = { 10, 20, 30, 40, 50 };
int n = sizeof (arr) / sizeof (arr[0]);
int key = 30;
printf ( "Array before deletion\n" );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
n = deleteElement(arr, n, key);
printf ( "\n\nArray after deletion\n" );
for (i = 0; i < n; i++)
printf ( "%d " , arr[i]);
}
|
Java
class Main {
static int binarySearch( int arr[], int low, int high,
int key)
{
if (high < low)
return - 1 ;
int mid = (low + high) / 2 ;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1 ), high, key);
return binarySearch(arr, low, (mid - 1 ), key);
}
static int deleteElement( int arr[], int n, int key)
{
int pos = binarySearch(arr, 0 , n - 1 , key);
if (pos == - 1 ) {
System.out.println( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1 ; i++)
arr[i] = arr[i + 1 ];
return n - 1 ;
}
public static void main(String[] args)
{
int i;
int arr[] = { 10 , 20 , 30 , 40 , 50 };
int n = arr.length;
int key = 30 ;
System.out.print( "Array before deletion:\n" );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
n = deleteElement(arr, n, key);
System.out.print( "\n\nArray after deletion:\n" );
for (i = 0 ; i < n; i++)
System.out.print(arr[i] + " " );
}
}
|
Python3
def deleteElement(arr, n, key):
pos = binarySearch(arr, 0 , n - 1 , key)
if (pos = = - 1 ):
print ( "Element not found" )
return n
for i in range (pos, n - 1 ):
arr[i] = arr[i + 1 ]
return n - 1
def binarySearch(arr, low, high, key):
if (high < low):
return - 1
mid = (low + high) / / 2
if (key = = arr[mid]):
return mid
if (key > arr[mid]):
return binarySearch(arr, (mid + 1 ), high, key)
return binarySearch(arr, low, (mid - 1 ), key)
if __name__ = = "__main__" :
arr = [ 10 , 20 , 30 , 40 , 50 ]
n = len (arr)
key = 30
print ( "Array before deletion" )
for i in range (n):
print (arr[i], end = " " )
n = deleteElement(arr, n, key)
print ( "\n\nArray after deletion" )
for i in range (n):
print (arr[i], end = " " )
|
C#
using System;
public class GFG {
static int binarySearch( int [] arr, int low, int high,
int key)
{
if (high < low)
return -1;
int mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
static int deleteElement( int [] arr, int n, int key)
{
int pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
Console.WriteLine( "Element not found" );
return n;
}
int i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
public static void Main()
{
int i;
int [] arr = { 10, 20, 30, 40, 50 };
int n = arr.Length;
int key = 30;
Console.Write( "Array before deletion:\n" );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
n = deleteElement(arr, n, key);
Console.Write( "\n\nArray after deletion:\n" );
for (i = 0; i < n; i++)
Console.Write(arr[i] + " " );
}
}
|
Javascript
<script>
function binarySearch(arr, low, high, key)
{
if (high < low)
return -1;
let mid = (low + high) / 2;
if (key == arr[mid])
return mid;
if (key > arr[mid])
return binarySearch(arr, (mid + 1), high, key);
return binarySearch(arr, low, (mid - 1), key);
}
function deleteElement( arr, n, key)
{
let pos = binarySearch(arr, 0, n - 1, key);
if (pos == -1) {
document.write( "Element not found" );
return n;
}
let i;
for (i = pos; i < n - 1; i++)
arr[i] = arr[i + 1];
return n - 1;
}
let i;
let arr = [ 10, 20, 30, 40, 50 ];
let n = arr.length;
let key = 30;
document.write( "Array before deletion:\n" );
for (i = 0; i < n; i++)
document.write(arr[i] + " " );
n = deleteElement(arr, n, key);
document.write( "<br>" + "Array after deletion:\n" );
for (i = 0; i < n; i++)
document.write(arr[i] + " " );
</script>
|
Output
Array before deletion
10 20 30 40 50
Array after deletion
10 20 40 50
Time Complexity: O(N). In the worst case all elements may have to be moved Auxiliary Space: O(log N). An implicit stack will be used
|