What Is An Array in Data Structures
What Is An Array in Data Structures
Python
Java
C++
import array
# Example:
arr = array.array('i', [1, 2, 5, 6])
Python
Java
C++
Output
2
a
b
c
0.8
Python
Java
C++
Output
Apple
Mango
Banana
Orange
Grapes
1.Insertion
We can insert one or multiple elements in an array as per the
requirement at the required positions or indexes.
// array size
int n = 5;
// index of element to be added
int pos = 2;
// element to be added
int item = 4;
Output
1 5
2 2
3 4
4 8
5 5
6 7
1.Deletion
It is used to delete an element from a particular index in an array.
Python
Java
C++
// array size
int n = 5;
// index of element to be deleted
int pos = 2;
// adjusting the value of loop counter to pos
int j = pos;
while (j < n) {
balls[j - 1] = balls[j];
j = j + 1;
}
Output
The chairs and the corresponding number of balls after removing a ball
1 5
2 8
3 5
4 7
1.Search
It is used to search an element using the given index or by the
value. We can search any element in an array and display both, its
index and value.
while (j < n) {
if (balls[j] == order)
break;
j = j + 1;
}
Output
The corresponding chair number of order:4 is -> 2
1.Update
We can change the values of the elements inside an array at any
position or index.
noOfStudents[pos] = item;
Output
The number of students sitting on the benches in order:
5
6
7
2
4
8
3
Bench numbers and the corresponding number of students sitting on each bench
1 5
2 6
3 7
4 2
5 4
6 8
7 3
Student Numbers and the updated no of Students on each bench after the 1st update
1 5
2 6
3 7
4 0
5 4
6 8
7 3