| 18 | */ |
| 19 | |
| 20 | class ArrayList { |
| 21 | constructor() { |
| 22 | this.length = 0; |
| 23 | this.data = {}; |
| 24 | } |
| 25 | push(value) { |
| 26 | this.data[this.length] = value; |
| 27 | this.length++; |
| 28 | } |
| 29 | pop() { |
| 30 | const ans = this.data[this.length - 1]; |
| 31 | delete this.data[this.length - 1]; |
| 32 | this.length--; |
| 33 | return ans; |
| 34 | } |
| 35 | get(index) { |
| 36 | return this.data[index]; |
| 37 | } |
| 38 | delete(index) { |
| 39 | const ans = this.data[index]; |
| 40 | this._collapseTo(index); |
| 41 | return ans; |
| 42 | } |
| 43 | _collapseTo(index) { |
| 44 | for (let i = index; i < this.length; i++) { |
| 45 | this.data[i] = this.data[i + 1]; |
| 46 | } |
| 47 | delete this.data[this.length - 1]; |
| 48 | this.length--; |
| 49 | } |
| 50 | serialize() { |
| 51 | return this.data; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | // unit tests |
| 56 | // do not modify the below code |
nothing calls this directly
no outgoing calls
no test coverage detected