Data Structure: Arrays

Seghosimhe David
CodeX
Published in
3 min readJun 25, 2022
progamming photo
Photo by Jexo on Unsplash

Grouping of items, materials, individuals e.t.c, is something we all are familiar with. In the world of computer science, grouping is also a common practice i.e collection of items, materials, data and many more can be done with the use of Arrays.

What is an Array?
An Array which is sometimes called “List” is simply a collection of items under a single variable name. examples are:

//The array below is a collection of names
var namesArray = ["Daniel", "David" , "Prince", "Janeth"]
var numberArray = [1, 2, 3, 4, 5]

Arrays store items in order i.e they arrange items sequentially. Because items are stored in order, the have the smallest footprint of any data structure.

Types of Arrays?
1. Static Arrays
2. Dynamic Arrays

Static Arrays: These are arrays which assign their memory at compile time and they also have a fixed size. A static array cannot be altered.

//Example:
int array[10]
//The array defined above creates an array of size 10 which means we can only input 10 items, an attempt to input the 11th item would fail.

Though static arrays may sound really boring and not flexible, it has some great advantages and also disadvantages.

Advantages of Static Arrays
1. Its execution time is quick.
2. Static allocation lasts for the duration of the program’s runtime.

Disadvantages of Static Arrays
1.
Waste of space occurs if more static data space than necessary is declared.
2. It is difficult to increase this fixed size during runtime if less static space than required is provided.

Languages which static array is common to are: C++, Java, e.t.c.

Dynamic Arrays: The dynamic array is an upgrade from the static array because it solves the limitation of memory allocation which is encounter with the static array. Memory is automatically assigned based on how the elements increase.

Advantages of Dynamic Arrays
1.
Quick lookup.
2. It is cache-friendly.

Disadvantages of Dynamic Arrays
1.
Issues may arise when we want to manage memory ourselves.

Languages which dynamic array is common to are: Javascript, Python, e.t.c.

Some Array operations and their Big-O notation:
1. Insert — O(n)
2. Delete — O(n)
3. Lookup — O(1)
4. Push — O(1)
5. Pop — O(1)
6. Unshift — O(n)
7. Shift — O(n)

Implementation of an Array using Javascript

class seghoArray {
constructor() {
this.length = 0;
this.data = {}
}
get(index){
return this.data[index];
}
push(entity) {
this.data[this.length] = entity;
this.length++
return this.length
}
pop() {
const lastItem = this.length-1
delete this.data[this.length-1]
this.length--
return lastItem
}
delete(index) {
const item = this.data[index]
this.shiftItems(index)
return item
}
unshift(item){
const tempArray = new seghoArray();
for (let i = 0; i < this.length; i++){
tempArray.data[i + 1] = this.data[i]
}

tempArray.data[0] = item
this.data = tempArray.data;
this.length++;
}shift(){
delete this.data[0]
for (let i = 0; i < this.length-1; i++){
this.data[i] = this.data[i + 1]
}
delete this.data[this.length-1]
this.length--
}
shiftItems(index) {
for (let i = index; i < this.length-1; i++){
this.data[i] = this.data[i + 1]
}
delete this.data[this.length-1]
this.length--
}
}
const array = new seghoArray()array.push('boy')
array.push('girl')
array.push('man')
array.push('many')
array.unshift('bro')
array.shift()
console.log(array)

To learn more about arrays, check here

Hope you enjoyed this brief introduction to Arrays!

--

--