argumentsLength that returns the count of arguments passed to it.
Example 1:
Input: args = [5] Output: 1 Explanation: argumentsLength(5); // 1 One value was passed to the function so it should return 1.
Example 2:
Input: args = [{}, null, "3"]
Output: 3
Explanation:
argumentsLength({}, null, "3"); // 3
Three values were passed to the function so it should return 3.
Constraints:
args is a valid JSON array0 <= args.length <= 100The key idea in #2703 Return Length of Arguments Passed is to determine how many parameters are provided when a function is called. In JavaScript, functions can receive a variable number of arguments, so the task focuses on counting them efficiently.
One common approach is to use the rest parameter syntax (...args), which gathers all passed arguments into an array-like structure. Once collected, you can simply determine the total count using the array's length property. Another approach relies on the built-in arguments object available inside traditional functions, which also stores all received parameters and exposes a length property.
Both approaches are straightforward and require only a constant-time operation to retrieve the count. Since no additional processing or iteration is needed, the solution remains highly efficient with minimal memory usage.
This approach leverages built-in properties or methods provided by programming languages to determine the number of arguments passed to a function. These features make it simple to count the number of arguments dynamically.
In C, we use the variadic function capability from the standard library. We define a function that takes at least one parameter, which will be the count of the arguments. The function simply returns this count.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1) - Consistently returns a given number.
Space Complexity: O(1) - No additional space needed beyond the function's internal variables.
This approach involves counting the arguments using a loop or other constructs to manually track the number of parameters. Although less commonly needed in modern languages due to built-in functionality, it's a potential way to understand argument storage deeply.
In C, leveraging variadic functions allows us to define a manual counting approach, but here we focus on providing the number directly or hardware constraints might require manual counting.
C++
Java
Python
C#
JavaScript
Time Complexity: O(1) - Due to usage constraints.
Space Complexity: O(1) - No additional space is used.
| Approach | Complexity |
|---|---|
| Using Built-in Methods | Time Complexity: O(1) - Consistently returns a given number. |
| Manual Count | Time Complexity: O(1) - Due to usage constraints. |
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Using Rest Parameters (...args) | O(1) | O(1) |
| Using the arguments Object | O(1) | O(1) |
I solved too many Leetcode problems • NeetCodeIO • 101,495 views views
Watch 9 more video solutions →Practice Return Length of Arguments Passed with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor