This content originally appeared on DEV Community and was authored by Piyush Acharya
Intuition
The problem is to find the length of the arguments passed to a function. A possible way to solve this problem is to use the rest parameter syntax (…args) to collect all the arguments into an array and then return the length of the array.
Approach
To implement this approach, we define a function named argumentsLength that takes any number of arguments using the rest parameter syntax. Inside the function, we simply return args.length, which is the length of the array that contains all the arguments.
Complexity
Time complexity: $$O(1)$$
Space complexity: $$O(n)$$
Code
/**
* @return {number}
*/
var argumentsLength = function(...args) {
return args.length;
};
/**
* argumentsLength(1, 2, 3); // 3
*/
This content originally appeared on DEV Community and was authored by Piyush Acharya
Piyush Acharya | Sciencx (2023-06-02T16:06:08+00:00) 2703. LeetCode’s Return Length of Arguments Passed – Extremely LOGICAL JS Solution Beats 83% in Memory Usage ✨. Retrieved from https://www.scien.cx/2023/06/02/2703-leetcodes-return-length-of-arguments-passed-extremely-logical-js-solution-beats-83-in-memory-usage-%e2%9c%a8/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.