Program to Delete Element From an Array by Location

Program to Delete Element From an Array by Location

  • Write a program to Delete Element From an Array by Location in C
  • Write a program to Delete Element From an Array by Location in C++
  • Write a program to Delete Element From an Array by Location in Python
  • Write a program to Delete Element From an Array by Location in PHP
  • Write a program to Delete Element From an Array by Location in Java
  • Write a program to Delete Element From an Array by Location in Java Script
  • Write a program to Delete Element From an Array by Location in C#

Explanation:

To delete an element from an array by its location (index), you can follow these steps:

Pseudocode Logic:

  1. Input: The array and the index of the element to delete.
  2. Check Boundaries: Ensure the index is valid (i.e., within the range of the array).
  3. Shift Elements: Move all elements after the target index one position to the left.
  4. Resize Array: Adjust the array size by removing the last element (optional for dynamic arrays in some programming languages).
  5. Output: The modified array.

Program to Delete Element From an Array by Location

#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int index = 2;  // Index of element to delete (0-based index)

    // Check if the index is valid
    if (index >= 0 && index < size) {
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];  // Shift elements to the left
        }
        size--;  // Decrease the size of the array
    }

    // Print the array after deletion
    printf("Array after deletion: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}

#include <iostream>
using namespace std;

int main() {
    int arr[] = {1, 2, 3, 4, 5};  // Example array
    int size = sizeof(arr) / sizeof(arr[0]);
    int index = 2;  // Index of element to delete (0-based index)

    // Check if the index is valid
    if (index >= 0 && index < size) {
        for (int i = index; i < size - 1; i++) {
            arr[i] = arr[i + 1];  // Shift elements to the left
        }
        size--;  // Decrease the size of the array
    }

    cout << "Array after deletion: ";
    for (int i = 0; i < size; i++) {
        cout << arr[i] << " ";
    }
    cout << endl;

    return 0;
}

arr = [1, 2, 3, 4, 5]  # Example array
index = 2  # Index of element to delete

# Check if the index is valid
if 0 <= index < len(arr):
    arr.pop(index)  # Remove the element at the specified index

print("Array after deletion:", arr)

<?php
$arr = array(1, 2, 3, 4, 5);  // Example array
$index = 2;  // Index of element to delete

// Check if the index is valid
if ($index >= 0 && $index < count($arr)) {
    array_splice($arr, $index, 1);  // Remove the element at the specified index
}

echo "Array after deletion: ";
print_r($arr);
?>

import java.util.ArrayList;
import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Integer[] arr = {1, 2, 3, 4, 5};  // Example array
        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(arr)); // Convert array to ArrayList
        int index = 2;  // Index of element to delete

        // Check if the index is valid
        if (index >= 0 && index < list.size()) {
            list.remove(index);  // Remove the element at the specified index
        }

        System.out.print("Array after deletion: ");
        for (int num : list) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}

let arr = [1, 2, 3, 4, 5];  // Example array
let index = 2;  // Index of element to delete

// Check if the index is valid
if (index >= 0 && index < arr.length) {
    arr.splice(index, 1);  // Remove element at the specified index
}

console.log("Array after deletion:", arr);

using System;
using System.Linq;

class Program {
    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};  // Example array
        int index = 2;  // Index of element to delete

        // Check if the index is valid
        if (index >= 0 && index < arr.Length) {
            arr = arr.Where((val, idx) => idx != index).ToArray();  // Remove the element at the specified index
        }

        Console.Write("Array after deletion: ");
        foreach (var num in arr) {
            Console.Write(num + " ");
        }
        Console.WriteLine();
    }
}

List of All Programs