Back to blog
Oct 17, 2024
6 min read

Is Object Empty

Given an object or an array, return true if it is empty, otherwise return false.

Difficulty: Easy | Acceptance: 81.90% | Paid: No Topics: N/A

Given an object or an array, return true if it is empty, otherwise return false.

The object or array is considered empty if it contains no key-value pairs or elements.

Examples

Example 1

Input: obj = {"x": 5, "y": 42}
Output: false
Explanation: The object has 2 key-value pairs.

Example 2

Input: obj = {}
Output: true
Explanation: The object has no key-value pairs.

Example 3

Input: obj = [null, false, 0]
Output: false
Explanation: The array has 3 elements.

Constraints

obj is a valid JSON object or array
2 <= JSON.stringify(obj).length <= 10^5

Approach 1: Using Object.keys()

Intuition Get all keys of the object and check if the array of keys is empty.

Steps

  • Use Object.keys() to get an array of all enumerable properties
  • Check if the length of this array is 0
  • Return true if length is 0, false otherwise
python
def isEmpty(obj):
    if isinstance(obj, dict):
        return len(obj) == 0
    elif isinstance(obj, list):
        return len(obj) == 0
    return True

Complexity

  • Time: O(n) where n is the number of keys
  • Space: O(n) for storing the keys array
  • Notes: Creates an intermediate array of keys

Approach 2: Using Object.values()

Intuition Get all values of the object and check if the array of values is empty.

Steps

  • Use Object.values() to get an array of all enumerable property values
  • Check if the length of this array is 0
  • Return true if length is 0, false otherwise
python
def isEmpty(obj):
    if isinstance(obj, dict):
        return len(obj) == 0
    elif isinstance(obj, list):
        return len(obj) == 0
    return True

Complexity

  • Time: O(n) where n is the number of values
  • Space: O(n) for storing the values array
  • Notes: Creates an intermediate array of values

Approach 3: Using Object.entries()

Intuition Get all key-value pairs of the object and check if the array of entries is empty.

Steps

  • Use Object.entries() to get an array of all enumerable property [key, value] pairs
  • Check if the length of this array is 0
  • Return true if length is 0, false otherwise
python
def isEmpty(obj):
    if isinstance(obj, dict):
        return len(obj) == 0
    elif isinstance(obj, list):
        return len(obj) == 0
    return True

Complexity

  • Time: O(n) where n is the number of entries
  • Space: O(n) for storing the entries array
  • Notes: Creates an intermediate array of [key, value] pairs

Approach 4: Using for…in loop

Intuition Iterate through all enumerable properties and check if any property exists.

Steps

  • Use a for…in loop to iterate through all enumerable properties
  • If any property is found, return false
  • If loop completes without finding any property, return true
python
def isEmpty(obj):
    if isinstance(obj, dict):
        return len(obj) == 0
    elif isinstance(obj, list):
        return len(obj) == 0
    return True

Complexity

  • Time: O(n) where n is the number of properties
  • Space: O(1) - no additional space used
  • Notes: Early termination when first property is found

Approach 5: Using JSON.stringify()

Intuition Convert the object to a JSON string and check if it equals "" or ”[]“.

Steps

  • Use JSON.stringify() to convert the object to a string
  • Check if the string is "" or ”[]“
  • Return true if either condition is met, false otherwise
python
import json

def isEmpty(obj):
    if isinstance(obj, dict):
        return len(obj) == 0
    elif isinstance(obj, list):
        return len(obj) == 0
    return True

Complexity

  • Time: O(n) where n is the size of the object
  • Space: O(n) for the JSON string
  • Notes: Less efficient due to string conversion overhead