DEV Community

Abhishek Sharma Gaur
Abhishek Sharma Gaur

Posted on

Arrays Data Structure in Javascript, Python and Ruby

An array is a collection of items stored in contiguous memory. The idea is to keep many items of the same type together. This makes it easy to calculate the position of each element by adding an offset to the base value, which is the memory location of the first element of the array (usually indicated by the name of the array).

Algorithm for PUSH operation

  • Check if the array is full or not.
  • If the array is full, exit the program.
  • If the array is not full, then increment the size and add the element to the end of the array.

Algorithm for POP operation

  • Check if the array is empty or not.
  • If the array is empty, print the underflow error and exit the program.
  • If the array is not empty, pop the last element.

Time complexity

Push: O(1)
Pop: O(1)
Size: O(1)
SearchByIndex: O(1)
SearchByValue: O(n)

The functions associated with stack are:

size() – Returns the length of the array

push(a) – Inserts the element at the end of an array.

pop() – Deletes the last element of the array.

searchByIndex - Identifies element on a known index.

searchByValue - Retrieve the value's index in the array.

print - To print the array

Below we have a Javascript program implementing an array data structure

class CustomArray {
    constructor(limit) {
      this.customArray = [];
      this.limit = 10
    }

    push = (item) => {
      if(this.customArray.length > this.limit) this.limit++
      this.customArray.push(item)
    };

    pop = () => this.customArray.pop();

    print = () => console.log("array:", JSON.stringify(this.customArray));

    searchByIndex = (index) => {
        console.log('searchByIndex for index', this.customArray[index]);
        return this.customArray[index]
    }

    searchByValue = (value) => {
        for(let x in this.customArray){
            if(this.customArray[x] === value) {
                console.log(`searchByValue for value ${x}`);
                return x
            }
        }
        return -1
    }
  }

  let customArray = new CustomArray();
  customArray.push(1);
  customArray.push(2);
  customArray.push(3);
  customArray.print();

  customArray.pop();
  customArray.print();
  customArray.searchByIndex(1);
  customArray.searchByValue(1);
Enter fullscreen mode Exit fullscreen mode

Below we have a Python program implementing an array data structure

class CustomArray:
    def __init__(self, limit):
        self.customArray = []
        self.limit = limit

    def push(self, item):
        if len(self.customArray) > self.limit:
            self.limit = self.limit + 1
        return self.customArray.insert(0, item)


    def pop(self):
        return self.customArray.pop(0)

    def size(self):
        return self.limit

    def print(self):
        return print("Stack is:", self.customArray)

    def searchByValue(self, value):
        print(self.customArray.index(value))
        return self.customArray.index(value)

    def searchByIndex(self, index):
        print(self.customArray[index])
        return self.customArray[index]

customArray = CustomArray(10)
customArray.push(1)
customArray.push(2)
customArray.push(3)
customArray.print()

customArray.pop()
customArray.print()
customArray.searchByIndex(1)
customArray.searchByValue(1)
Enter fullscreen mode Exit fullscreen mode

Below we have a Ruby program implementing an array data structure

class CustomArray
    def initialize(limit)
        @@customArray = []
        @@limit = limit
    end

    def pop
        if @@customArray.length > 0
            @@customArray.pop
        end
    end

    def push(item)
        @@customArray.push(item)
    end

    def print
        puts "#{@@customArray}"
    end

    def searchByIndex(index)
        puts @@customArray.at(index)
    end

    def searchByValue(item)
        puts @@customArray.find_index(item)
    end

    def size
        puts "#{@@limit}"
    end    
end

arr = CustomArray.new(10)
arr.push(1);
arr.push(2);
arr.push(3);
arr.print();

arr.pop();
arr.print();
arr.searchByIndex(1);
arr.searchByValue(1);
arr.size

Enter fullscreen mode Exit fullscreen mode

Top comments (0)