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++ program to implement linear// search in unsorted array#include <bits/stdc++.h>using namespace std;Â
// Function to implement search operationint findElement(int arr[], int n, int key){    int i;    for (i = 0; i < n; i++)        if (arr[i] == key)            return i;           // If the key is not found    return -1;}Â
// Driver's Codeint main(){Â Â Â Â int arr[] = { 12, 34, 10, 6, 40 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    // Using a last element as search element    int key = 40;         // Function call    int position = findElement(arr, n, key);Â
    if (position == -1)        cout << "Element not found";    else        cout << "Element Found at Position: "             << position + 1;Â
    return 0;}Â
// This code is contributed// by Akanksha Rai |
C
// C program to implement linear// search in unsorted array#include <stdio.h>Â
// Function to implement search operationint findElement(int arr[], int n, int key){    int i;    for (i = 0; i < n; i++)        if (arr[i] == key)            return i;           // If the key is not found    return -1;}Â
// Driver's Codeint main(){Â Â Â Â int arr[] = { 12, 34, 10, 6, 40 };Â Â Â Â int n = sizeof(arr) / sizeof(arr[0]);Â
    // Using a last element as search element    int key = 40;         // Function call    int position = findElement(arr, n, key);Â
    if (position == -1)        printf("Element not found");    else        printf("Element Found at Position: %d",               position + 1);Â
    return 0;} |
Java
// Java program to implement linear// search in unsorted arraysÂ
class Main {       // Function to implement    // search operation    static int findElement(int arr[], int n, int key)    {        for (int i = 0; i < n; i++)            if (arr[i] == key)                return i;Â
          // If the key is not found        return -1;    }Â
    // Driver's Code    public static void main(String args[])    {        int arr[] = { 12, 34, 10, 6, 40 };        int n = arr.length;Â
        // Using a last element as search element        int key = 40;                 // Function call        int position = findElement(arr, n, key);Â
        if (position == -1)            System.out.println("Element not found");        else            System.out.println("Element Found at Position: "                               + (position + 1));    }} |
Python3
# Python program for searching in# unsorted arrayÂ
Â
def findElement(arr, n, key):    for i in range(n):        if (arr[i] == key):            return i               # If the key is not found    return -1Â
Â
# Driver's codeif __name__ == '__main__':Â Â Â Â arr = [12, 34, 10, 6, 40]Â Â Â Â key = 40Â Â Â Â n = len(arr)Â
    # search operation    index = findElement(arr, n, key)    if index != -1:        print("Element Found at position: " + str(index + 1))    else:        print("Element not found")Â
    # Thanks to Aditi Sharma for contributing    # this code |
C#
// C# program to implement linear// search in unsorted arraysusing System;Â
class main {    // Function to implement    // search operation    static int findElement(int[] arr, int n, int key)    {        for (int i = 0; i < n; i++)            if (arr[i] == key)                return i;                   // If the key is not found        return -1;    }Â
    // Driver Code    public static void Main()    {        int[] arr = { 12, 34, 10, 6, 40 };        int n = arr.Length;Â
        // Using a last element as        // search element        int key = 40;        int position = findElement(arr, n, key);Â
        if (position == -1)            Console.WriteLine("Element not found");        else            Console.WriteLine("Element Found at Position: "                              + (position + 1));    }}Â
//Â This code is contributed by vt_m. |
Javascript
// Javascript program to implement linear // search in unsorted arrayÂ
Â
// Function to implement search operation function findElement( arr, n, key){Â Â Â Â let i;Â Â Â Â for (i = 0; i < n; i++)Â Â Â Â Â Â Â Â if (arr[i] == key)Â Â Â Â Â Â Â Â Â Â Â Â return i;Â
    return -1;}Â
Â
         // Driver program         let arr = [12, 34, 10, 6, 40];    let n = arr.length;Â
    // Using a last element as search element    let key = 40;    let position = findElement(arr, n, key);Â
    if (position == - 1)        document.write("Element not found");    else        document.write("Element Found at Position: "             + (position + 1)); |
PHP
<?php// PHP program to implement linear // search in unsorted arrayÂ
// Function to implement// search operation function findElement($arr, $n, $key){    $i;    for ($i = 0; $i < $n; $i++)        if ($arr[$i] == $key)            return $i;           // If the key is not found    return -1;}Â
// Driver Code$arr = array(12, 34, 10, 6, 40);$n = sizeof($arr);Â
// Using a last element// as search element$key = 40;$position = findElement($arr, $n, $key);Â
if ($position == - 1)    echo("Element not found");else    echo("Element Found at Position: " . ($position + 1));Â
// This code is contributed by Ajit.?> |
Element Found at Position: 5
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Insert Operation:
1. Insert at the end:
In an unsorted array, the insert operation is faster as compared to a sorted array because we don’t have to care about the position at which the element is to be placed.
Coding implementation of inserting an element at the end:
C
// C program to implement insert// operation in an unsorted array.#include <stdio.h>Â
// Inserts a key in arr[] of given capacity.// n is current size of arr[]. This// function returns n + 1 if insertion// is successful, else n.int insertSorted(int arr[], int n, int key, int capacity){Â
    // Cannot insert more elements if n is    // already more than or equal to capacity    if (n >= capacity)        return n;Â
    arr[n] = key;Â
    return (n + 1);}Â
// Driver Codeint main(){Â Â Â Â int arr[20] = { 12, 16, 20, 40, 50, 70 };Â Â Â Â int capacity = sizeof(arr) / sizeof(arr[0]);Â Â Â Â int n = 6;Â Â Â Â int i, key = 26;Â
    printf("\n Before Insertion: ");    for (i = 0; i < n; i++)        printf("%d ", arr[i]);Â
    // Inserting key    n = insertSorted(arr, n, key, capacity);Â
    printf("\n After Insertion: ");    for (i = 0; i < n; i++)        printf("%d ", arr[i]);Â
    return 0;} |
Java
// Java program to implement insert// operation in an unsorted array.Â
class Main {    // Function to insert a given key in    // the array. This function returns n+1    // if insertion is successful, else n.    static int insertSorted(int arr[], int n, int key,                            int capacity)    {Â
        // Cannot insert more elements if n        // is already more than or equal to        // capacity        if (n >= capacity)            return n;Â
        arr[n] = key;Â
        return (n + 1);    }Â
    // Driver Code    public static void main(String[] args)    {        int[] arr = new int[20];        arr[0] = 12;        arr[1] = 16;        arr[2] = 20;        arr[3] = 40;        arr[4] = 50;        arr[5] = 70;        int capacity = 20;        int n = 6;        int i, key = 26;Â
        System.out.print("Before Insertion: ");        for (i = 0; i < n; i++)            System.out.print(arr[i] + " ");Â
        // Inserting key        n = insertSorted(arr, n, key, capacity);Â
        System.out.print("\n After Insertion: ");        for (i = 0; i < n; i++)            System.out.print(arr[i] + " ");    }} |
Python3
# Python program for inserting# an element in an unsorted arrayÂ
# method to insert elementÂ
Â
def insert(arr, element):Â Â Â Â arr.append(element)Â
Â
# Driver's codeif __name__ == '__main__':    # declaring array and key to insert    arr = [12, 16, 20, 40, 50, 70]    key = 26Â
    # array before inserting an element    print("Before Inserting: ")    print(arr)Â
    # array after Inserting element    insert(arr, key)    print("After Inserting: ")    print(arr)Â
    # Thanks to Aditi Sharma for contributing    # this code |
C#
// C# program to implement insert// operation in an unsorted array.using System;Â
class main {Â
    // Function to insert a given    // key in the array. This    // function returns n + 1    // if insertion is successful,    // else n.    static int insertSorted(int[] arr, int n, int key,                            int capacity)    {Â
        // Cannot insert more elements        // if n is already more than        // or equal to capacity        if (n >= capacity)            return n;Â
        arr[n] = key;        return (n + 1);    }Â
    // Driver Code    public static void Main()    {        int[] arr = new int[20];        arr[0] = 12;        arr[1] = 16;        arr[2] = 20;        arr[3] = 40;        arr[4] = 50;        arr[5] = 70;        int capacity = 20;        int n = 6;        int i, key = 26;Â
        Console.Write("Before Insertion: ");        for (i = 0; i < n; i++)            Console.Write(arr[i] + " ");        Console.WriteLine();Â
        // Inserting key        n = insertSorted(arr, n, key, capacity);Â
        Console.Write("After Insertion: ");        for (i = 0; i < n; i++)            Console.Write(arr[i] + " ");    }}Â
// This code is contributed by vt_m. |
Javascript
// Javascript program to implement insert// operation in an unsorted array.Â
// Function to insert a given// key in the array. This// function returns n + 1// if insertion is successful,// else n.function insertSorted(arr, n, key, capacity){          // Cannot insert more elements    // if n is already more than    // or equal to capacity    if (n >= capacity)        return n;      arr[n] = key;    return (n + 1);}Â
let arr = new Array(20);arr[0] = 12;arr[1] = 16;arr[2] = 20;arr[3] = 40;arr[4] = 50;arr[5] = 70;let capacity = 20;let n = 6;let i, key = 26;Â
document.write("Before Insertion: ");for (i = 0; i < n; i++)Â Â document.write(arr[i]+" ");document.write("</br>");Â
// Inserting keyn = insertSorted(arr, n, key, capacity);Â
document.write("After Insertion: ");for (i = 0; i < n; i++)Â Â document.write(arr[i]+" "); |
PHP
<?php// PHP program to implement insert // operation in an unsorted array. Â
// Inserts a key in arr[] of given // capacity. n is current size of arr[]. // This function returns n + 1 if // insertion is successful, else n. function insertSorted(&$arr, $n, $key, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $capacity) { Â
    // Cannot insert more elements if n is     // already more than or equal to capacity     if ($n >= $capacity)         return $n; Â
    array_push($arr, $key); Â
    return ($n + 1); } Â
// Driver CodeÂ
$arr = array(12, 16, 20, 40, 50, 70); $capacity = 20; $n = 6; $key = 26; Â
echo "Before Insertion: "; for ($i = 0; $i < $n; $i++) Â Â Â Â echo $arr[$i] . " "; Â
// Inserting key $n = insertSorted($arr, $n, Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â $key, $capacity); Â
echo "\nAfter Insertion: "; for ($i = 0; $i < $n; $i++) Â Â Â Â echo $arr[$i] . " "; Â Â Â Â Â // This code is contributed by// Rajput-Ji?> |
Before Insertion: 12 16 20 40 50 70 After Insertion: 12 16 20 40 50 70 26
Time Complexity: O(1)Â
Auxiliary Space: O(1)
2. Insert at any position
Insert operation in an array at any position can be performed by shifting elements to the right, which are on the right side of the required position
Coding implementation of inserting an element at any position:
C++
// C++ Program to Insert an element// at a specific position in an ArrayÂ
#include <bits/stdc++.h>using namespace std;Â
// Function to insert element// at a specific positionvoid insertElement(int arr[], int n, int x, int pos){    // shift elements to the right    // which are on the right side of pos    for (int i = n - 1; i >= pos; i--)        arr[i + 1] = arr[i];Â
    arr[pos] = x;}Â
// Driver's codeint main(){Â Â Â Â int arr[15] = { 2, 4, 1, 8, 5 };Â Â Â Â int n = 5;Â
    cout<<"Before insertion : ";    for (int i = 0; i < n; i++)        cout<<arr[i]<<" ";Â
    cout<<endl;Â
    int x = 10, pos = 2;         // Function call    insertElement(arr, n, x, pos);    n++;Â
    cout<<"After insertion : ";    for (int i = 0; i < n; i++)        cout<<arr[i]<<" ";Â
    return 0;} |
C
// C Program to Insert an element// at a specific position in an ArrayÂ
#include <stdio.h>Â
// Function to insert element// at a specific positionvoid insertElement(int arr[], int n, int x, int pos){    // shift elements to the right    // which are on the right side of pos    for (int i = n - 1; i >= pos; i--)        arr[i + 1] = arr[i];Â
    arr[pos] = x;}Â
// Driver's codeint main(){Â Â Â Â int arr[15] = { 2, 4, 1, 8, 5 };Â Â Â Â int n = 5;Â
    printf("Before insertion : ");    for (int i = 0; i < n; i++)        printf("%d ", arr[i]);Â
    printf("\n");Â
    int x = 10, pos = 2;         // Function call    insertElement(arr, n, x, pos);    n++;Â
    printf("After insertion : ");    for (int i = 0; i < n; i++)        printf("%d ", arr[i]);Â
    return 0;} |
Java
/*package whatever //do not write package name here */import java.io.*;Â
// Java Program to Insert an element// at a specific position in an Arrayclass GFG {    static void insertElement(int arr[], int n, int x,                              int pos)    {        // shift elements to the right        // which are on the right side of pos        for (int i = n - 1; i >= pos; i--)            arr[i + 1] = arr[i];        arr[pos] = x;    }    public static void main(String[] args)    {        int arr[] = new int[15];        arr[0] = 2;        arr[1] = 4;        arr[2] = 1;        arr[3] = 8;        arr[4] = 5;        int n = 5;        int x = 10, pos = 2;Â
        System.out.print("Before Insertion: ");        for (int i = 0; i < n; i++)            System.out.print(arr[i] + " ");Â
        // Inserting key at specific position        insertElement(arr, n, x, pos);        n += 1;Â
        System.out.print("\n\nAfter Insertion: ");        for (int i = 0; i < n; i++)            System.out.print(arr[i] + " ");    }}Â
// This code is contributed by syedsarfarazahammed |
Python3
# python Program to Insert an element# at a specific position in an Arraydef insertElement(arr, n, x, pos) :         # shift elements to the right    # which are on the right side of pos    for i in range(n-1,pos-1,-1) :        arr[i + 1] = arr[i]             arr[pos] = xÂ
# Driver's codeif __name__ == '__main__':    # Declaring array and key to delete    # here -1 is for empty space    arr = [2, 4, 1, 8, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]    n = 5         print("Before insertion : ")    for i in range(0,n) :        print(arr[i],end=' ')Â
    print("\n")Â
    x = 10;    pos = 2;       # Function call    insertElement(arr, n, x, pos);    n+=1Â
    print("After insertion : ")    for i in range(0,n) :        print(arr[i],end=' ')          #This Code is contributed by aditya942003patil |
C#
// C# program to implement insert// operation in an unsorted array.using System;Â
class main {Â
  static void insertElement(int[] arr, int n, int x,                            int pos)  {    // shift elements to the right    // which are on the right side of pos    for (int i = n - 1; i >= pos; i--)      arr[i + 1] = arr[i];    arr[pos] = x;  }Â
  public static void Main()  {Â
    int[] arr = new int[20];    arr[0] = 2;    arr[1] = 4;    arr[2] = 1;    arr[3] = 8;    arr[4] = 5;    int x = 10;    int n = 5;    int pos = 2;Â
    Console.Write("Before Insertion: ");    for (int i = 0; i < n; i++)      Console.Write(arr[i] + " ");    Console.WriteLine();Â
    // Inserting key at specific position    insertElement(arr, n, x, pos);    n += 1;Â
    Console.Write("\n\nAfter Insertion: ");    for (int i = 0; i < n; i++)      Console.Write(arr[i] + " ");  }}Â
// This code is contributed by sourabhdalal0001 |
Javascript
// javascript Program to Insert an element// at a specific position in an Array    function insertElement(arr, n, x, pos)    {             // shift elements to the right        // which are on the right side of pos        var i = n - 1;        for (i; i >= pos; i--)        {            arr[i + 1] = arr[i];        }        arr[pos] = x;    }             var arr = Array(15).fill(0);        arr[0] = 2;        arr[1] = 4;        arr[2] = 1;        arr[3] = 8;        arr[4] = 5;        var n = 5;        var x = 10;        var pos = 2;        console.log("Before Insertion: ");        var i = 0;        for (i; i < n; i++)        {            console.log(arr[i] + " ");        }                 // Inserting key at specific position        insertElement(arr, n, x, pos);        n += 1;        console.log("\n\nAfter Insertion: ");        i = 0;        for (i; i < n; i++)        {            console.log(arr[i] + " ");        }                 // This code is contributed by sourabhdalal0001. |
PHP
<?php// Function to insert element at a specific positionfunction insertElement(&$arr, $n, $x, $pos) {    // shift elements to the right which are on the right side of pos    for ($i = $n - 1; $i >= $pos; $i--) {        $arr[$i + 1] = $arr[$i];    }Â
    // insert the new element at the specified position    $arr[$pos] = $x;}Â
// Driver's code$arr = array(2, 4, 1, 8, 5);$n = 5;Â
echo "Before insertion : ";for ($i = 0; $i < $n; $i++) {Â Â Â Â echo $arr[$i] . " ";}Â
echo "\n";Â
$x = 10;$pos = 2;Â
// Function callinsertElement($arr, $n, $x, $pos);$n++;Â
echo "After insertion : ";for ($i = 0; $i < $n; $i++) {Â Â Â Â echo $arr[$i] . " ";}?> |
Before insertion : 2 4 1 8 5 After insertion : 2 4 10 1 8 5
Time complexity: O(N)
Auxiliary Space: O(1)
Delete Operation:
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++
// C++ program to implement delete operation in a// unsorted array#include <iostream>using namespace std;Â
// To search a key to be deletedint findElement(int arr[], int n, int key);Â
// Function to delete an elementint deleteElement(int arr[], int n, int key){    // Find position of element to be deleted    int pos = findElement(arr, n, key);Â
    if (pos == -1) {        cout << "Element not found";        return n;    }Â
    // Deleting element    int i;    for (i = pos; i < n - 1; i++)        arr[i] = arr[i + 1];Â
    return n - 1;}Â
// Function to implement search operationint findElement(int arr[], int n, int key){Â Â Â Â int i;Â Â Â Â for (i = 0; i < n; i++)Â Â Â Â Â Â Â Â if (arr[i] == key)Â Â Â Â Â Â Â Â Â Â Â Â return i;Â
    return -1;}Â
// Driver's codeint main(){Â Â Â Â int i;Â Â Â Â int arr[] = { 10, 50, 30, 40, 20 };Â
    int n = sizeof(arr) / sizeof(arr[0]);    int key = 30;Â
    cout << "Array before deletion\n";    for (i = 0; i < n; i++)        cout << arr[i] << " ";              // Function call    n = deleteElement(arr, n, key);Â
    cout << "\n\nArray after deletion\n";    for (i = 0; i < n; i++)        cout << arr[i] << " ";Â
    return 0;}Â
// This code is contributed by shubhamsingh10 |
C
// C program to implement delete operation in a// unsorted array#include <stdio.h>Â
// To search a key to be deletedint findElement(int arr[], int n, int key);Â
// Function to delete an elementint deleteElement(int arr[], int n, int key){    // Find position of element to be deleted    int pos = findElement(arr, n, key);Â
    if (pos == -1) {        printf("Element not found");        return n;    }Â
    // Deleting element    int i;    for (i = pos; i < n - 1; i++)        arr[i] = arr[i + 1];Â
    return n - 1;}Â
// Function to implement search operationint findElement(int arr[], int n, int key){Â Â Â Â int i;Â Â Â Â for (i = 0; i < n; i++)Â Â Â Â Â Â Â Â if (arr[i] == key)Â Â Â Â Â Â Â Â Â Â Â Â return i;Â
    return -1;}Â
// Driver's codeint main(){Â Â Â Â int i;Â Â Â Â int arr[] = { 10, 50, 30, 40, 20 };Â
    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]);           // Function call    n = deleteElement(arr, n, key);Â
    printf("\nArray after deletion\n");    for (i = 0; i < n; i++)        printf("%d ", arr[i]);Â
    return 0;} |
Java
// Java program to implement delete// operation in an unsorted arrayÂ
class Main {    // function to search a key to    // be deleted    static int findElement(int arr[], int n, int key)    {        int i;        for (i = 0; i < n; i++)            if (arr[i] == key)                return i;Â
        return -1;    }Â
    // Function to delete an element    static int deleteElement(int arr[], int n, int key)    {        // Find position of element to be        // deleted        int pos = findElement(arr, n, key);Â
        if (pos == -1) {            System.out.println("Element not found");            return n;        }Â
        // Deleting element        int i;        for (i = pos; i < n - 1; i++)            arr[i] = arr[i + 1];Â
        return n - 1;    }Â
    // Driver's Code    public static void main(String args[])    {        int i;        int arr[] = { 10, 50, 30, 40, 20 };Â
        int n = arr.length;        int key = 30;Â
        System.out.println("Array before deletion");        for (i = 0; i < n; i++)            System.out.print(arr[i] + " ");                   // Function call        n = deleteElement(arr, n, key);Â
        System.out.println("\n\nArray after deletion");        for (i = 0; i < n; i++)            System.out.print(arr[i] + " ");    }} |
Python3
# Python program to delete an element# from an unsorted arrayÂ
# Driver's codeif __name__ == '__main__':    # Declaring array and key to delete    arr = [10, 50, 30, 40, 20]    key = 30      print("Array before deletion:")    print (arr)      # deletes key if found in the array     # otherwise shows error not in list    arr.remove(key)    print("Array after deletion")    print(arr)      # This code is contributed by Aditi Sharma. |
C#
// C# program to implement delete// operation in an unsorted arrayusing System;Â
class main {    // Function to search a    // key to be deleted    static int findElement(int[] arr, int n, int key)    {Â
        int i;        for (i = 0; i < n; i++)            if (arr[i] == key)                return i;Â
        return -1;    }Â
    // Function to delete an element    static int deleteElement(int[] arr, int n, int key)    {        // Find position of element        // to be deleted        int pos = findElement(arr, n, key);Â
        if (pos == -1) {            Console.WriteLine("Element not found");            return n;        }Â
        // Deleting element        int i;        for (i = pos; i < n - 1; i++)            arr[i] = arr[i + 1];Â
        return n - 1;    }Â
    // Driver Code    public static void Main()    {        int i;        int[] arr = { 10, 50, 30, 40, 20 };Â
        int n = arr.Length;        int key = 30;Â
        Console.Write("Array before deletion ");        for (i = 0; i < n; i++)            Console.Write(arr[i] + " ");        Console.WriteLine();Â
          // Function call        n = deleteElement(arr, n, key);Â
        Console.Write("Array after deletion ");        for (i = 0; i < n; i++)            Console.Write(arr[i] + " ");    }}Â
// This code is contributed by vt_m. |
Javascript
// Java script program to implement delete// operation in an unsorted array  Â
    // function to search a key to     // be deleted    function findElement(arr,n,key)    {        let i;        for (i = 0; i < n; i++)            if (arr[i] == key)                return i;              return -1;    }          // Function to delete an element    function deleteElement(arr,n,key)    {        // Find position of element to be         // deleted        let pos = findElement(arr, n, key);              if (pos == -1)        {            document.write("Element not found");            return n;        }              // Deleting element        let i;        for (i = pos; i< n - 1; i++)            arr[i] = arr[i + 1];              return n - 1;    }          // Driver Code             let i;        let arr = [10, 50, 30, 40, 20];              let n = arr.length;        let key = 30;              document.write("Array before deletion<br>");        for (i=0; i<n; i++)          document.write(arr[i] + " ");              n = deleteElement(arr, n, key);             document.write("<br><br>Array after deletion<br>");        for (i=0; i<n; i++)          document.write(arr[i]+" ");     // This code is contributed by sravan kumar Gottumukkala |
PHP
<?php// PHP program to implement delete // operation in an unsorted array Â
// To search a key to be deleted function findElement(&$arr, $n, $key){ Â Â Â Â for ($i = 0; $i < $n; $i++) Â Â Â Â Â Â Â Â if ($arr[$i] == $key) Â Â Â Â Â Â Â Â Â Â Â Â return $i; Â
    return -1; } Â
// Function to delete an element function deleteElement(&$arr, $n, $key) {     // Find position of element to     // be deleted     $pos = findElement($arr, $n, $key); Â
    if ($pos == -1)     {         echo "Element not found";         return $n;     } Â
    // Deleting element     for ($i = $pos; $i < $n - 1; $i++)         $arr[$i] = $arr[$i + 1]; Â
    return $n - 1; } Â
// Driver code $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] . " "; Â
// Function call$n = deleteElement($arr, $n, $key); Â
echo "\nArray after deletion\n"; for ($i = 0; $i < $n; $i++) echo $arr[$i] . " "; Â
// This code is contributed by// Rajput-Ji?> |
Array before deletion 10 50 30 40 20 Array after deletion 10 50 40 20
Time Complexity: O(N)Â
Auxiliary Space: O(1)
Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!




