Talentd
FleetCode

2727. Is Object Empty

Easy80.8% acceptance1 company

Given an object or an array, return if it is 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.

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 array
  • 2 <= JSON.stringify(obj).length <= 105
Can you solve it in O(1) time?

Asked at 1 company

9:48
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1/**
2 * @param {Object|Array} obj
3 * @return {boolean}
4 */
5var isEmpty = function(obj) {
6
7};
Starter codeJavaScriptSpaces: 4
Examples
Input
{"x": 5, "y": 42}
Input
{}
Input
[null, false, 0]
Your code is saved on this device.Solution