Efficient Array Deletion : End, Beginning, and Arbitrary Position

Efficient Array Deletion : End, Beginning, and Arbitrary Position

Data Structure Algorithm

Introduction

Arrays are fundamental data structures in programming, and understanding how to efficiently delete elements from them is crucial. In this blog post, we'll explore three common scenarios: deleting an element at the end, at the beginning, and at an arbitrary position (kth position) in a C++ array.

Deleting at the End

Deleting an element at the end of an array involves simply reducing the size of the array, effectively excluding the last element.

Pseudocode:

1. Decrement the size of the array.
2. The element at the last index is now considered removed.

C++ Program:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Deleting at the end
    size--;

    // Print the modified array
    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Time Complexity:

The time complexity for deleting an element at the end is O(1) since it involves a simple size decrement.

Space Complexity:

The space complexity is also O(1) as it does not require additional memory.

Deleting at the Beginning

Deleting an element at the beginning of an array involves shifting all elements one position to the left and reducing the size of the array.

Pseudocode:

1. Shift all elements one position to the left.
2. Decrement the size of the array.

C++ Program:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);

    // Deleting at the beginning
    for (int i = 0; i < size - 1; ++i) {
        arr[i] = arr[i + 1];
    }
    size--;

    // Print the modified array
    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Time Complexity:

The time complexity for deleting an element at the beginning is O(n) since it involves shifting all elements.

Space Complexity:

The space complexity is O(1) as it does not require additional memory.

Deleting at Arbitrary Position (kth Position)

Deleting an element at an arbitrary position (kh position) involves shifting all elements from the specified position to the end one position to the left and reducing the size of the array.

Pseudocode:

1. Shift all elements from kh position to the end one position to the left.
2. Decrement the size of the array.

C++ Program:

#include <iostream>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int size = sizeof(arr) / sizeof(arr[0]);
    int kh = 2;  // Position to delete

    // Deleting at kh position
    for (int i = kh; i < size - 1; ++i) {
        arr[i] = arr[i + 1];
    }
    size--;

    // Print the modified array
    for (int i = 0; i < size; ++i) {
        std::cout << arr[i] << " ";
    }

    return 0;
}

Time Complexity:

The time complexity for deleting an element at an arbitrary position is O(n) since it involves shifting elements.

Space Complexity:

The space complexity is O(1) as it does not require additional memory.

Understanding these methods provides a solid foundation for manipulating arrays in C++. Use them wisely based on the specific requirements of your program. Happy coding!

Did you find this article valuable?

Support Xander | ᓕᘏᗢ by becoming a sponsor. Any amount is appreciated!