Remove an array element and shift the remaining ones
You just need to overwrite what you're deleting with the next value in the array, propagate that change, and then keep in mind where the new end is:
int array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
// delete 3 (index 2)
for (int i = 2; i < 8; ++i)
array[i] = array[i + 1]; // copy next element left
Now your array is {1, 2, 4, 5, 6, 7, 8, 9, 9}
. You cannot delete the extra 9
since this is a statically-sized array, you just have to ignore it. This can be done with std::copy
:
std::copy(array + 3, // copy everything starting here
array + 9, // and ending here, not including it,
array + 2) // to this destination
In C++11, use can use std::move
(the algorithm overload, not the utility overload) instead.
More generally, use std::remove
to remove elements matching a value:
// remove *all* 3's, return new ending (remaining elements unspecified)
auto arrayEnd = std::remove(std::begin(array), std::end(array), 3);
Even more generally, there is std::remove_if
.
Note that the use of std::vector<int>
may be more appropriate here, as its a "true" dynamically-allocated resizing array. (In the sense that asking for its size()
reflects removed elements.)
Delete array element and shift the rest of it to the left
The method you're looking for is splice
: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
arr.splice(index, 1)
C - delete element from array and reorganise
Your array is statically allocated, so always has the same size and deleted elements have the 0
value (according how you define the deleted values).
This link can help you and explains about how to delete element from array.
Javascript Remove an array element and reorder the remaining elements
You can use Array.prototype.slice
(which does not mutate the original array unlike splice
) to take a copy of the array from the 1st position.
Then use Array.from
to get a new array from the copy and mention the length
property value of the old array:
const arr = [1, 2, 3, 4, 5, 6]const newArr = Array.from({length: arr.length, ...arr.slice(1)});console.log(newArr);
How to shift elements to left after removing array element?
Here is another variant:
int[] arr = new int[] { 1, 2, 30, 4, 5, 30, 6 };
int k = 30;
int j = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] != k) {
arr[j++] = arr[i];
}
}
while (j < arr.length) {
arr[j++] = 0;
}
Delete array element - move all elements down one index
This was my final solution:
static int nameArrayCount, markArrayCount = 0;
static void deleteStudent() {
System.out.println("Which student would you like to delete?");
for(int i=0;i<10;i++) {
System.out.println(i + ": " + studentNamesArray[i]);
}
int studentChoice = input.nextInt();
for(int i = studentChoice+1; i<studentNamesArray.length; i++) {
studentNamesArray[i-1] = studentNamesArray[i];
}
nameArrayCount = nameArrayCount -1;
}
How to Remove Array Element and Then Re-Index Array?
unset($foo[0]); // remove item at index 0
$foo2 = array_values($foo); // 'reindex' array
Related Topics
How to Immediately Invoke a C++ Lambda
I Want a Vector of Derived Class Pointers as Base Class Pointers
Get Pointer to Object from Pointer to Some Member
How to Use C++ with Objective-C in Xcode
Why Does Calling Method Through Null Pointer "Work" in C++
What's the Fastest Way to Pack 32 0/1 Values into the Bits of a Single 32-Bit Variable
How to Make Reading from 'Std::Cin' Timeout After a Particular Amount of Time
What's the Precedence of Comma Operator Inside Conditional Operator in C++
Is This Undefined Behavior with Const_Cast
Good Debugger Tutorial for Beginners
Significance of Parentheses in Decltype((C))
How to Reverse the Order of Arguments of a Variadic Template Function
How to Use Createthread for Functions Which Are Class Members
Openmp Set_Num_Threads() Is Not Working
Rotate Cv::Mat Using Cv::Warpaffine Offsets Destination Image