
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Delete array element in given index range [L – R] in C++?
Let us first define the original array and the exclusive range for deletion of the array elements and also find the original array length −
int arr[] = { 2,4,6,8,10,12,14,16,18,20}; int L = 2, R = 6; int length = sizeof(arr) / sizeof(arr[0]);
Now we loop in the array and if index position (i) is greater than L or R we increment the variable k which will be used to shift positions(delete) of array element once the index value (i) lies between the range L and R. Also, the new length of the given array will be k.
int k = 0; for (int i = 0; i < length; i++) { if (i <= L || i >= R) { arr[k] = arr[i]; k++; } }
Example
Let us see the following implementation to get a better understanding of deleting array elements in given index
#include <iostream> using namespace std; int main() { int arr[] = { 2,4,6,8,10,12,14,16,18,20}; int L = 2, R = 6; int length = sizeof(arr) / sizeof(arr[0]); int k = 0; for (int i = 0; i < length; i++) { if (i <= L || i >= R) { arr[k] = arr[i]; k++; } } length=k; for (int i = 0; i < length; i++) cout << arr[i] << " "; return 0; }
Output
The above code will produce the following output −
2 4 6 14 16 18 20
- Related Articles
- Delete array element in given index range [L – R] in C++ Program
- Get index of given element in array field in MongoDB?
- How to delete an element from an array in PHP and re-index the array?
- Queries for bitwise AND in the index range [L, R] of the given Array using C++
- Queries for Bitwise OR in the Index Range [L, R] of the Given Array using C++
- Golang program to delete the ith index node, when index is the out of range in the linked list.
- Delete elements in range in Python
- How to delete element from an array in MongoDB?
- Queries to update a given index and find gcd in range in C++
- How to delete n-th element of array in MongoDB?
- Inserting element at falsy index in an array - JavaScript
- Queries to update a given index and find gcd in range in C++ Program
- How to delete an array element based on key in PHP?
- Finding the index of last element in the array in C#
- Finding the index of first element in the array in C#

Advertisements