Skip to content

Commit 2f62166

Browse files
committed
feat: Implement deletion from array by shifting elements left
🧠 Logic: - Deletes an element at a specific index by shifting all subsequent elements one position to the left. - Reduces the logical size of the array to reflect the deletion. - Demonstrates a basic array deletion algorithm without using built-in methods. Signed-off-by: Somesh diwan <[email protected]>
1 parent 26209cb commit 2f62166

File tree

1 file changed

+19
-16
lines changed

1 file changed

+19
-16
lines changed
Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
/*
2-
Deletion from an array
3-
Deleting elements from a 1D array involves removing an element from a specific position within the array and then shifting the remaining elements to fill the gap.
2+
Deletion from an array:
3+
Deleting elements from a 1D array involves removing an element from a
4+
specific position within the array and then shifting the remaining elements to fill the gap.
45
56
Here's a step-by-step process for deleting elements from a 1D array:
67
7-
Determine the Position: Decide which element you want to delete from the array. You need to know the index (position) of the element you want to remove.
8-
Shift Elements: Starting from the position of the element to be deleted, move each element one position to the left until you reach the end of the array. This fills the gap left by the deleted element.
9-
Update Array Size: If your array is dynamically allocated or you're using a mechanism that tracks the size, decrease the size of the array to reflect the new number of elements.
10-
Task
11-
You are given the code in the IDE.
12-
Update the code based on the steps given above to get the necessary output!*/
8+
Determine the Position: Decide which element you want to delete from the array.
9+
You need to know the index (position) of the element you want to remove.
10+
Shift Elements: Starting from the position of the element to be deleted,
11+
move each element one position to the left until you reach the end of the array.
12+
13+
This fills the gap left by the deleted element.
14+
Update Array Size: If your array is dynamically allocated, or you're using a mechanism that tracks the size,
15+
decrease the size of the array to reflect the new number of elements.
16+
17+
Task, You are given the code in the IDE.
18+
Update the code based on the steps given above to get the necessary output!
19+
*/
1320

1421
public class ArrayIMPCC1{
1522
public static void main(String[] args) {
@@ -22,20 +29,16 @@ public static void main(String[] args) {
2229
int size = 5;
2330

2431
int positionToDelete = 2;
25-
//index of element to delete the position
32+
//index of an element to delete the position
2633

27-
for(int i = positionToDelete; i < size -1; i++)
28-
{
34+
for(int i = positionToDelete; i < size -1; i++) {
2935
array[i] = array[i+1];
3036
}
31-
3237
size --;
3338

3439
//updated array
35-
36-
for (int i = 0; i < size; i++)
37-
{
40+
for (int i = 0; i < size; i++) {
3841
System.out.print(array[i]+" ");
3942
}
4043
}
41-
}
44+
}

0 commit comments

Comments
 (0)