An array is a data structure that stores a collection of data values in contiguous memory locations.
let array = [4,3,8,1,0,14,6];
Memory Location | Value |
---|---|
1000 | 4 |
1001 | 3 |
1002 | 8 |
1003 | 1 |
1004 | 0 |
1005 | 14 |
1006 | 6 |
Accessing an element in an array is done by indexing into it.
array[0] // 4 (index 0 is the first element) (1000)
array[1] // 3 (index 1 is the second element) (1001)
array[2] // 8 (index 2 is the third element) (1002)
array[3] // 1 (index 3 is the fourth element) (1003)
array[4] // 0 (index 4 is the fifth element) (1004)
array[5] // 14 (index 5 is the sixth element) (1005)
array[6] // 6 (index 6 is the seventh element) (1006)
Operation | Time Complexity |
---|---|
Accessing an element | O(1) |
Searching an element | O(N) |
Inserting an element | O(N) |
Deleting an element | O(N) |