Implement a Stack class with `push(val)`, `pop()`, `peek()`, `isEmpty()`, and `size()` methods.
Problem Statement
Implement a Stack class with `push(val)`, `pop()`, `peek()`, `isEmpty()`, and `size()` methods.
Complexity
Time Complexity: -
Space Complexity: -
Hints
š” Hint 1:
A stack follows Last-In-First-Out (LIFO) principle.
š” Hint 2:
Use an array internally. push() adds to end, pop() removes from end.
š” Hint 3:
peek() returns the last element without removing it.
ā
Solution:
```javascript
class Stack {
constructor() { this.items = []; }
push(val) { this.items.push(val); }
pop() { return this.items.pop(); }
peek() { return this.items[this.items.length - 1]; }
isEmpty() { return this.items.length === 0; }
size() { return this.items.length; }
}
```