Quick actions

cmd+k|ctrl+k

Navigation

Languages

Implementing an array

Snippet info

Language

JavaScript

Visibility

public

Author

hamzo864

Created

2023-10-03T01:13:16.036891Z

Updated

2023-10-03T01:31:03.439852Z

class MyArray {
    constructor(){
        this.length = 0;
        this.data = {};
    }
    
    get(index) {
        return this.data[index]
    }
    
    push(item){
        this.data[this.length] = item;
        this.length++
    }
    
    pop(){
        const lastItem = this.data[this.length - 1]
        delete this.data[this.length-1]
        this.length--
        return lastItem
    }
    
    delete(index){
        const item = this.data[index];
        this.shiftItems(index);
        
    }
    
    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 newArray = new MyArray();
newArray.push(3)
newArray.push(2)
newArray.push(5)
console.log(newArray.get(0))
console.log(newArray.length)

newArray.delete(1)
console.log(newArray)
INFO