Skip to content

Commit d95e553

Browse files
authored
Update Array.md
1 parent 862eb4c commit d95e553

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Array.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,44 @@ int main()
124124

125125
}
126126
```
127+
### Deletion
128+
Deletion refers to removing an existing element from the array and re-organizing all elements of an array.
129+
130+
Following is the implementation of the above algorithm −
131+
132+
```c
133+
#include<stdio.h>
134+
int main()
135+
136+
{
137+
int array[5] = { 2, 6, 8, 3, 9};
138+
139+
// initial size of array
140+
int n = 5;
141+
int i = 0;
142+
143+
// element to be deleted
144+
int x = 6;
145+
int index;
146+
147+
// iterate the array elements using loop if any element matches the key, store the index
148+
149+
for (i = 0; i < n-1; i++)
150+
{
151+
if (array[i] == x)
152+
{
153+
index = i;
154+
break;
155+
}
156+
}
157+
158+
//shift all the element from index+1 by one position to the left
159+
for (i = index; i < n-1; i++)
160+
array[i] = array[i+1];
161+
162+
// print new array
163+
for (i = 0; i < n-1; i++)
164+
printf("%d ",array[i]);
165+
166+
}
167+
```

0 commit comments

Comments
 (0)