File tree Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Expand file tree Collapse file tree 1 file changed +57
-0
lines changed Original file line number Diff line number Diff line change
1
+ // Question Link: https://leetcode.com/problems/is-object-empty/description/?envType=study-plan-v2&envId=30-days-of-javascript
2
+ // Solution Link: https://leetcode.com/problems/is-object-empty/solutions/5446306/2-easy-one-line-javascript-solution-using-loops-and-predefined-function/
3
+
4
+ /*
5
+ 2727. Is Object Empty
6
+
7
+ Given an object or an array, return if it is empty.
8
+ An empty object contains no key-value pairs.
9
+ An empty array contains no elements.
10
+ You may assume the object or array is the output of JSON.parse.
11
+
12
+ Example 1:
13
+ Input: obj = {"x": 5, "y": 42}
14
+ Output: false
15
+ Explanation: The object has 2 key-value pairs so it is not empty.
16
+
17
+ Example 2:
18
+ Input: obj = {}
19
+ Output: true
20
+ Explanation: The object doesn't have any key-value pairs so it is empty.
21
+
22
+ Example 3:
23
+ Input: obj = [null, false, 0]
24
+ Output: false
25
+ Explanation: The array has 3 elements so it is not empty.
26
+
27
+ Constraints:
28
+ obj is a valid JSON object or array
29
+ 2 <= JSON.stringify(obj).length <= 10^5
30
+
31
+ Can you solve it in O(1) time?
32
+ */
33
+
34
+
35
+
36
+ /**
37
+ * @param {Object|Array } obj
38
+ * @return {boolean }
39
+ */
40
+
41
+ // 1st Approach
42
+ var isEmpty = function ( obj ) {
43
+ for ( const _ in obj ) {
44
+ return false ;
45
+ }
46
+
47
+ return true ;
48
+ } ;
49
+
50
+ /*
51
+ // 2nd Approach
52
+ var isEmpty = function(obj) {
53
+ let result = Object.keys(obj).length === 0;
54
+
55
+ return result;
56
+ };
57
+ */
You can’t perform that action at this time.
0 commit comments