Skip to content

Commit 8ed4fc7

Browse files
committed
feat: Implement element deletion from array by index with left shift
🧠 Logic: - Initialize an array `A` with 6 elements and size tracker `n = 6`. - To delete an element at a given `index`, shift all subsequent elements one position to the left. - Decrease the array size (`n--`) to exclude the last redundant value. - Print the array before and after deletion to visualize the change. Signed-off-by: Somesh diwan <[email protected]>
1 parent 22961f3 commit 8ed4fc7

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed
Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
public class DeleteAnElementInArray {
2-
public static void main(String[] args)
3-
{
2+
public static void main(String[] args) {
43
int A[] = new int[10];
54

65
A[0] = 3; A[1] = 9; A[2] = 7; A[3] = 8; A[4] = 12; A[5] = 6;
@@ -14,22 +13,20 @@ public static void main(String[] args)
1413

1514

1615
int index = 2; // Index of the element to be deleted
17-
1816
// Shifting elements to the left to overwrite the element at the given index
19-
for (int i = index; i < n - 1; i++)
20-
{
17+
for (int i = index; i < n - 1; i++) {
2118
A[i] = A[i + 1];
2219
}
23-
2420
n--; // Decrement the size of the array after deletion
2521

26-
// Print array after deletion
2722
for (int i = 0; i < n; i++)
2823
System.out.print(A[i] + ",");
2924
System.out.println("");
3025
}
3126
}
3227

33-
/*The element at the specified index is deleted by shifting all SubSequent elements one position to the left.
28+
/*
29+
The element at the specified index is deleted by shifting all SubSequent elements one position to the left.
3430
The variable n is decremented to reflect the new size of the array.
35-
The result is printed before and after deletion to compare the changes.*/
31+
The result is printed before and after deletion to compare the changes.
32+
*/

0 commit comments

Comments
 (0)