Given an object or an array, return if it is empty.
You may assume the object or array is the output of JSON.parse.
Example 1:
Input: obj = {"x": 5, "y": 42}
Output: false
Explanation: The object has 2 key-value pairs so it is not empty.
Example 2:
Input: obj = {}
Output: true
Explanation: The object doesn't have any key-value pairs so it is empty.
Example 3:
Input: obj = [null, false, 0] Output: false Explanation: The array has 3 elements so it is not empty.
Constraints:
obj is a valid JSON object or array2 <= JSON.stringify(obj).length <= 105Can you solve it in O(1) time?
Problem Overview: You receive an object or array and need to determine whether it contains any elements. Return true if it has no enumerable keys or items, otherwise return false.
Approach 1: Using Size/Length Properties (O(n) time, O(1) space)
The most common solution checks how many keys exist in the object. In JavaScript this typically means calling Object.keys(obj) and inspecting the resulting array length. If the length is 0, the object has no properties. Arrays follow the same idea using their length field. The algorithm internally iterates through the object's enumerable keys, giving a time complexity of O(n) where n is the number of properties, while using constant auxiliary space aside from the key list created by the runtime.
This approach is concise and readable, which is why it appears frequently in production code and interviews. It relies on built-in language helpers that already implement efficient iteration over object properties.
Approach 2: Using Iterator Method (O(n) time, O(1) space)
Another approach directly iterates through the object using a loop such as for...in (JavaScript) or language-specific iterators in C++, Java, or Python. The key idea is early termination: the moment you encounter the first property, you know the object is not empty and can immediately return false. If the loop finishes without visiting any property, the object is empty.
This method avoids creating an intermediate list of keys and may exit earlier in practice if the object contains elements. The worst-case complexity is still O(n) because every key might need to be checked. The space complexity remains O(1) since no additional data structures are required. The technique relies on fundamental data structure traversal patterns and is useful when working close to the runtime or when minimizing allocations.
Recommended for interviews: Both solutions are acceptable. The size/length property approach is the most recognizable and communicates the intent immediately, so many interviewers expect it first. The iterator-based approach demonstrates deeper understanding of how property traversal works and can be slightly more memory-efficient. Showing both approaches proves you understand object traversal and basic algorithm complexity tradeoffs.
This approach involves using language-specific properties or methods that can quickly determine if an object or array has any elements. For objects, this often involves checking the number of keys, while for arrays it's about checking the length.
In this hypothetical C solution, we are assuming the use of some JSON library where JSON objects and arrays have defined length and size. The isObjectEmpty function checks if the length (or size) is zero to determine emptiness.
Time Complexity: O(1) because we are directly accessing built-in properties.
Space Complexity: O(1) since no extra space is used except for few variables.
This method relies on the use of iterators to quickly assess emptiness by attempting to iterate over the first element. If there is no element to begin with, it returns empty.
This is a more experimental C solution assuming some JSON library where iterators can be compared to check for zero elements (i.e., iterators at the beginning equal iterators at the end).
Time Complexity: O(1).
Space Complexity: O(1).
TypeScript
JavaScript
| Approach | Complexity |
|---|---|
| Using Size/Length Properties | Time Complexity: O(1) because we are directly accessing built-in properties. |
| Using Iterator Method | Time Complexity: O(1). |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Size/Length Properties | O(n) | O(1) | Most readable solution; preferred in interviews and common production code. |
| Using Iterator Method | O(n) | O(1) | Useful when you want early exit without allocating a list of keys. |
Is Object Empty | Leetcode 2727 | JSON | 30 Days of JavaScript #javascript #leetcode • Learn With Chirag • 1,657 views views
Watch 6 more video solutions →Practice Is Object Empty with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor