Skip to content

Instantly share code, notes, and snippets.

@darshna09
Created April 19, 2021 09:11
Show Gist options
  • Save darshna09/2dac497602fe7c5c1d41cabce421002b to your computer and use it in GitHub Desktop.
Save darshna09/2dac497602fe7c5c1d41cabce421002b to your computer and use it in GitHub Desktop.
// Implement Array in JavaScript
class OurArray {
constructor () {
this.length = 0;
this.data = {}; // An array in JavaScript is Object
}
/** Lookup: Time Complexity = O(1)
* @param {number} index
* @return {any} item
*/
get (index) {}
/**
* Time Complexity = O(1)
* @param {any} item
* @return {number} length
* New length of the array
*/
push (item) {}
/**
* Time Complexity = O(1)
* @return {number} lastItem
*/
pop () {}
/**
* Time Complexity = O(N)
* @param {number} index
* Index of the item to be deleted
* @return {any} item
* Deleted item
*/
deleteAtIndex (index) {}
}
const myThings = new OurArray();
myThings; // { length: 0, data: {} }
myThings.get(0); // undefined
myThings.push('Books'); // 1
myThings.push('Car'); // 2
myThings.pop(); // 'Car'
myThings.push('House'); // 2
myThings.deleteAtIndex(1); // 'House'
myThings.get(0); // 'Books'
myThings; // { length: 1, data: { '0': 'Books' } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment