Given an array and a number ‘x’, write a function to delete ‘x’ from the given array. We assume that array maintains two things with it, capacity and size. So when we remove an item, capacity does not change, only size changes.
Example:
Input: arr[] = {3, 1, 2, 5, 90}, x = 2, size = 5, capacity = 5
Output: arr[] = {3, 1, 5, 90, _}, size = 4, capacity = 5
Input: arr[] = {3, 1, 2, _, _}, x = 2, size = 3, capacity = 5
Output: arr[] = {3, 1, _, _, _}, size = 2, capacity = 5
Method 1(First Search, then Remove): We first search ‘x’ in array, then elements that are on right side of x to one position back. The following are the implementation of this simple approach.
Implementation:
C
#include <stdio.h>
int deleteElement( int arr[], int n, int x)
{
int i;
for (i = 0; i < n; i++) {
if (arr[i] == x) {
break ;
}
}
if (i < n) {
n = n - 1;
for ( int j = i; j < n; j++) {
arr[j] = arr[j + 1];
}
}
return n;
}
int main()
{
int arr[] = { 11, 15, 6, 8, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 6;
n = deleteElement(arr, n, x);
printf ( "Modified array is \n" );
for ( int i = 0; i < n; i++) {
printf ( "%d " , arr[i]);
}
return 0;
}
|
C++
#include<bits/stdc++.h>
using namespace std;
int deleteElement( int arr[], int n, int x)
{
int i;
for (i=0; i<n; i++)
if (arr[i] == x)
break ;
if (i < n)
{
n = n - 1;
for ( int j=i; j<n; j++)
arr[j] = arr[j+1];
}
return n;
}
int main()
{
int arr[] = {11, 15, 6, 8, 9, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 6;
n = deleteElement(arr, n, x);
cout << "Modified array is \n" ;
for ( int i=0; i<n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.io.*;
class Deletion {
static int deleteElement( int arr[], int n, int x)
{
int i;
for (i= 0 ; i<n; i++)
if (arr[i] == x)
break ;
if (i < n)
{
n = n - 1 ;
for ( int j=i; j<n; j++)
arr[j] = arr[j+ 1 ];
}
return n;
}
public static void main(String[] args)
{
int arr[] = { 11 , 15 , 6 , 8 , 9 , 10 };
int n = arr.length;
int x = 6 ;
n = deleteElement(arr, n, x);
System.out.println( "Modified array is" );
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i]+ " " );
}
}
|
Python3
def deleteElement(arr, n, x):
for i in range (n):
if (arr[i] = = x):
break
if (i < n):
n = n - 1 ;
for j in range (i, n, 1 ):
arr[j] = arr[j + 1 ]
return n
if __name__ = = '__main__' :
arr = [ 11 , 15 , 6 , 8 , 9 , 10 ]
n = len (arr)
x = 6
n = deleteElement(arr, n, x)
print ( "Modified array is" )
for i in range (n):
print (arr[i], end = " " )
|
C#
using System;
class GFG {
static int deleteElement( int []arr,
int n, int x)
{
int i;
for (i = 0; i < n; i++)
if (arr[i] == x)
break ;
if (i < n)
{
n = n - 1;
for ( int j = i; j < n; j++)
arr[j] = arr[j+1];
}
return n;
}
public static void Main()
{
int []arr = {11, 15, 6, 8, 9, 10};
int n = arr.Length;
int x = 6;
n = deleteElement(arr, n, x);
Console.WriteLine( "Modified array is" );
for ( int i = 0; i < n; i++)
Console.Write(arr[i]+ " " );
}
}
|
Javascript
<script>
function deleteElement( arr, n, x)
{
let i;
for (i=0; i<n; i++)
if (arr[i] == x)
break ;
if (i < n)
{
n = n - 1;
for (let j=i; j<n; j++)
arr[j] = arr[j+1];
}
return n;
}
let arr = [11, 15, 6, 8, 9, 10];
let n = arr.length;
let x = 6;
n = deleteElement(arr, n, x);
document.write( "Modified array is </br>" );
for (let i=0; i<n; i++)
document.write(arr[i] + " " );
</script>
|
OutputModified array is
11 15 8 9 10
Time Complexity : O(n)
Auxiliary Space : O(1)
Method 2 (Move elements while searching): This method assumes that the element is always present in array. The idea is to start from right most element and keep moving elements while searching for ‘x’. Below are C++ and Java implementations of this approach. Note that this approach may give unexpected result when ‘x’ is not present in array.
Implementation:
C
#include <stdio.h>
int deleteElement( int arr[], int n, int x)
{
if (arr[n - 1] == x)
return (n - 1);
int prev = arr[n - 1], i;
for (i = n - 2; i >= 0 && arr[i] != x; i--) {
int curr = arr[i];
arr[i] = prev;
prev = curr;
}
if (i < 0)
return 0;
arr[i] = prev;
return (n - 1);
}
int main()
{
int arr[] = { 11, 15, 6, 8, 9, 10 };
int n = sizeof (arr) / sizeof (arr[0]);
int x = 6;
n = deleteElement(arr, n, x);
printf ( "Modified array is \n" );
for ( int i = 0; i < n; i++)
printf ( "%d " , arr[i]);
return 0;
}
|
C++
#include<iostream>
using namespace std;
int deleteElement( int arr[], int n, int x)
{
if (arr[n-1] == x)
return (n-1);
int prev = arr[n-1], i;
for (i=n-2; i>=0 && arr[i]!=x; i--)
{
int curr = arr[i];
arr[i] = prev;
prev = curr;
}
if (i < 0)
return 0;
arr[i] = prev;
return (n-1);
}
int main()
{
int arr[] = {11, 15, 6, 8, 9, 10};
int n = sizeof (arr)/ sizeof (arr[0]);
int x = 6;
n = deleteElement(arr, n, x);
cout << "Modified array is \n" ;
for ( int i=0; i<n; i++)
cout << arr[i] << " " ;
return 0;
}
|
Java
import java.io.*;
class Deletion
{
static int deleteElement( int arr[], int n, int x)
{
if (arr[n- 1 ] == x)
return (n- 1 );
int prev = arr[n- 1 ], i;
for (i=n- 2 ; i>= 0 && arr[i]!=x; i--)
{
int curr = arr[i];
arr[i] = prev;
prev = curr;
}
if (i < 0 )
return 0 ;
arr[i] = prev;
return (n- 1 );
}
public static void main(String[] args)
{
int arr[] = { 11 , 15 , 6 , 8 , 9 , 10 };
int n = arr.length;
int x = 6 ;
n = deleteElement(arr, n, x);
System.out.println( "Modified array is" );
for ( int i = 0 ; i < n; i++)
System.out.print(arr[i]+ " " );
}
}
|
Python3
def deleteElement(arr,n,x):
if arr[n - 1 ] = = x:
return n - 1
prev = arr[n - 1 ]
for i in range (n - 2 , 1 , - 1 ):
if arr[i]! = x:
curr = arr[i]
arr[i] = prev
prev = curr
if i< 0 :
return 0
arr[i] = prev
return n - 1
arr = [ 11 , 15 , 6 , 8 , 9 , 10 ]
n = len (arr)
x = 6
n = deleteElement(arr,n,x)
print ( "Modified array is" )
for i in range (n):
print (arr[i],end = " " )
|
C#
using System;
class GFG {
static int deleteElement( int []arr,
int n,
int x)
{
if (arr[n - 1] == x)
return (n - 1);
int prev = arr[n - 1], i;
for (i = n - 2; i >= 0 &&
arr[i] != x; i--)
{
int curr = arr[i];
arr[i] = prev;
prev = curr;
}
if (i < 0)
return 0;
arr[i] = prev;
return (n - 1);
}
public static void Main()
{
int []arr = {11, 15, 6, 8, 9, 10};
int n = arr.Length;
int x = 6;
n = deleteElement(arr, n, x);
Console.WriteLine( "Modified array is" );
for ( int i = 0; i < n; i++)
Console.Write(arr[i]+ " " );
}
}
|
Javascript
<script>
function deleteElement(arr,n,x)
{
if (arr[n-1] == x)
return (n-1);
let prev = arr[n-1], i;
for (i=n-2; i>=0 && arr[i]!=x; i--)
{
let curr = arr[i];
arr[i] = prev;
prev = curr;
}
if (i < 0)
return 0;
arr[i] = prev;
return (n-1);
}
let arr = [11, 15, 6, 8, 9, 10];
let n = arr.length;
let x = 6;
n = deleteElement(arr, n, x);
document.write( "Modified array is<br>" );
for (let i = 0; i < n; i++)
document.write(arr[i]+ " " );
</script>
|
OutputModified array is
11 15 8 9 10
Time Complexity : O(n)
Auxiliary Space : O(1)
Deleting an element from an array takes O(n) time even if we are given index of the element to be deleted. The time complexity remains O(n) for sorted arrays as well.
In linked list, if we know the pointer to the previous node of the node to be deleted, we can do deletion in O(1) time.
This article is contributed by Himanshu. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.