Quick actions

cmd+k|ctrl+k

Navigation

Languages

Array Implementation

Snippet info

Language

JavaScript

Visibility

public

Author

soham57

Created

2022-04-03T06:03:21.138297Z

Updated

2022-04-03T06:03:21.138297Z

class MyArray{
    constructor(){
        this.length=0;
        this.data={};
    }
    
    get(index){
        return this.data[index];
    }
    
    push(data){
        this.data[this.length]=data;
        this.length++;
        return this.data;
    }
    
    pop(){
        const lastItem=this.data[this.length-1];
        delete this.data[this.length-1];
        this.length--;
        return lastItem;
    }
    
    deleteAtIndex(index){
        const item= this.data[index];
        this.shiftArray(index);
        return item;
    }
    
    shiftArray(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 myArray = new MyArray();
myArray.push('hi');
myArray.push('you');
myArray.push('!');
myArray.pop();
myArray.deleteAtIndex(0);
myArray.push('are');
myArray.push('nice');
myArray.shiftArray(0);
console.log(myArray);
INFO