2619. Array Prototype Last LeetCode Solution
In this guide, you will get 2619. Array Prototype Last LeetCode Solution with the best time and space complexity. The solution to Array Prototype Last problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Array Prototype Last solution in C++
- Array Prototype Last solution in Java
- Array Prototype Last solution in Python
- Additional Resources
Problem Statement of Array Prototype Last
Write code that enhances all arrays such that you can call the array.last() method on any array and it will return the last element. If there are no elements in the array, it should return -1.
You may assume the array is the output of JSON.parse.
Example 1:
Input: nums = [null, {}, 3]
Output: 3
Explanation: Calling nums.last() should return the last element: 3.
Example 2:
Input: nums = []
Output: -1
Explanation: Because there are no elements, return -1.
Constraints:
arr is a valid JSON array
0 <= arr.length <= 1000
Complexity Analysis
- Time Complexity: Google AdSense
- Space Complexity: Google Analytics
2619. Array Prototype Last LeetCode Solution in C++
declare global {
interface Array<T> {
last(): T | -1;
}
}
Array.prototype.last = function () {
return this.length === 0 ? -1 : this[this.length - 1];
};
export {};
/* code provided by PROGIEZ */
2619. Array Prototype Last LeetCode Solution in Java
N/A
// code provided by PROGIEZ
2619. Array Prototype Last LeetCode Solution in Python
N/A
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.