Is Object Empty - Problem
Given an object or an array, return true if it is empty, false otherwise.
Definition of empty:
- An empty object contains no key-value pairs
- An empty array contains no elements
You may assume the object or array is the output of JSON.parse.
Input & Output
Example 1 — Empty Object
$
Input:
obj = {}
›
Output:
true
💡 Note:
The object has no key-value pairs, so it's empty and we return true
Example 2 — Non-empty Array
$
Input:
obj = [null, false, 0]
›
Output:
false
💡 Note:
The array contains elements (even though they're falsy values), so it's not empty
Example 3 — Empty Array
$
Input:
obj = []
›
Output:
true
💡 Note:
The array contains no elements, so it's empty and we return true
Constraints
- obj is a valid JSON object or array
- 2 ≤ JSON.stringify(obj).length ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input
Object or array from JSON.parse
2
Check
Determine if container has any items
3
Output
Return boolean true/false
Key Takeaway
🎯 Key Insight: Use built-in size methods to efficiently check if containers are empty
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code