Created
April 19, 2021 09:11
-
-
Save darshna09/2dac497602fe7c5c1d41cabce421002b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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