dynamic array Algorithm

A dynamic array algorithm is a data structure that provides an efficient way of managing and organizing data in computer memory. It is a hybrid between static arrays and linked lists, combining the advantages of both. Unlike static arrays, dynamic arrays can automatically resize themselves as elements are added or removed, making them a more flexible and powerful tool for handling data. This automatic resizing is achieved through the use of algorithms that allocate and deallocate memory as needed, ensuring that the array always has enough space to store its elements. At the core of the dynamic array algorithm is the concept of doubling the size of the array when it reaches its capacity. When the array becomes full, the algorithm allocates a new block of memory that is twice the size of the current array, and then copies the elements from the old array to the new one. This ensures that the dynamic array always has room to grow, while minimizing the number of memory reallocations required. To maintain efficiency, the array can also be shrunk when a certain percentage of its capacity is unused, typically by halving the size of the array. This helps to conserve memory and prevent wasted space. Overall, the dynamic array algorithm provides a flexible and efficient solution for managing data in computer applications, allowing developers to work with arrays that can grow and shrink in size as needed.
package dynamic-array

import (
	"errors"
)

var defaultCapacity = 10

type dynamicArray struct {
	size        int
	capacity    int
	elementData []interface{}
}

func (da *dynamicArray) put(index int, element interface{}) error {
	err := da.checkRangeFromIndex(index)

	if err != nil {
		return err
	}

	da.elementData[index] = element

	return nil
}

func (da *dynamicArray) add(element interface{}) {
	if da.size == da.capacity {
		da.newCapacity()
	}

	da.elementData[da.size] = element
	da.size++
}

func (da *dynamicArray) remove(index int) error {
	err := da.checkRangeFromIndex(index)

	if err != nil {
		return err
	}

	copy(da.elementData[index:], da.elementData[index+1:da.size])
	da.elementData[da.size-1] = nil

	da.size--

	return nil
}

func (da *dynamicArray) get(index int) (interface{}, error) {
	err := da.checkRangeFromIndex(index)

	if err != nil {
		return nil, err
	}

	return da.elementData[index], nil
}

func (da *dynamicArray) isEmpty() bool {
	return da.size == 0
}

func (da *dynamicArray) getData() []interface{} {
	return da.elementData[:da.size]
}

func (da *dynamicArray) checkRangeFromIndex(index int) error {
	if index >= da.size || index < 0 {
		return errors.New("index out of range")
	}
	return nil
}

func (da *dynamicArray) newCapacity() {
	if da.capacity == 0 {
		da.capacity = defaultCapacity
	} else {
		da.capacity = da.capacity << 1
	}

	newDataElement := make([]interface{}, da.capacity)

	copy(newDataElement, da.elementData)

	da.elementData = newDataElement
}

// func main() {
// 	numbers := dynamicArray{}
// 	fmt.Println(numbers.isEmpty())

// 	numbers.add(10)
// 	numbers.add(20)
// 	numbers.add(30)
// 	numbers.add(40)
// 	numbers.add(50)

// 	fmt.Println(numbers.isEmpty())

// 	fmt.Println(numbers.getData())

// 	numbers.remove(1)

// 	fmt.Println(numbers.getData())

// 	numberFound, _ := numbers.get(1)
// 	fmt.Println(numberFound)

// 	numbers.put(0, 100)
// 	fmt.Println(numbers.getData())
// }

LANGUAGE:

DARK MODE: