Given an object or array obj, return a compact object.
A compact object is the same as the original object, except with keys containing falsy values removed. This operation applies to the object and any nested objects. Arrays are considered objects where the indices are keys. A value is considered falsy when Boolean(value) returns false.
You may assume the obj is the output of JSON.parse. In other words, it is valid JSON.
Example 1:
Input: obj = [null, 0, false, 1] Output: [1] Explanation: All falsy values have been removed from the array.
Example 2:
Input: obj = {"a": null, "b": [false, 1]}
Output: {"b": [1]}
Explanation: obj["a"] and obj["b"][0] had falsy values and were removed.
Example 3:
Input: obj = [null, 0, 5, [0], [false, 16]] Output: [5, [], [16]] Explanation: obj[0], obj[1], obj[3][0], and obj[4][0] were falsy and removed.
Constraints:
obj is a valid JSON object2 <= JSON.stringify(obj).length <= 106The key idea behind solving #2705 Compact Object is to recursively traverse the given structure and remove all falsy values such as false, 0, null, "", undefined, and NaN. Since the input can be either an array or an object, your approach must handle both structures dynamically.
If the current value is an array, iterate through each element, recursively compact nested arrays or objects, and keep only elements that evaluate to truthy. If the value is an object, iterate through its keys and recursively process nested values before deciding whether to keep them.
This problem is best solved using recursion combined with conditional checks on value types. By visiting each element once and rebuilding the structure without falsy entries, you achieve an efficient traversal. The overall complexity depends on the number of elements processed in the object tree.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Recursive traversal of arrays and objects | O(n) | O(n) |
NeetCode
This approach leverages a recursive depth-first search to traverse the object or array. We remove any keys or indices associated with falsy values during traversal. The recursion ensures nested structures are appropriately compacted.
The time complexity is O(n), where n is the total number of elements (including nested) in the object. The space complexity also depends on the depth of recursion, primarily O(d) where d is the depth of nested objects.
1#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <stdbool.h>
5
6bool is_falsy(void *value, char type) {
7 switch (type) {
8 case 'n': return value == NULL;
9 case 'b': return (*(bool*)value) == false;
10 case 'i': return (*(int*)value) == 0;
11 case 's': return strcmp((char*)value, "") == 0;
12 default: return false;
13 }
14}
15
16void compact_object(void *obj, char type) {
17 // Fill with logic to handle recursively compacting the object or array in 'C'.
18 // This demo focuses on conceptual pseudocode, and actual implementation will
19 // require detailed type-checking and parsing logic.
20}
21
22int main() {
23 // Example usage and testing with pseudo objects here - actual implementation required.
24 return 0;
25}In C, handling JSON-like objects requires defining detailed structures for objects and arrays. This fragment outlines a basic setup for type checking and falsy determination. Complete recursive traversal and compaction logic would be needed for a working solution.
Instead of recursion, this approach uses an iterative depth-first search pattern using a stack to manage the traversal. It ensures that nested structures are compacted by maintaining traversal state without deep recursion, mitigating possible stack overflows in languages with limited recursion depth control.
Time Complexity: O(n) as each element is processed once; Space Complexity: O(n) where n encapsulates all elements since the entire structure can potentially end up in the stack.
1def compact_iterative(obj):
2 stack =
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
The optimal approach uses recursion to traverse arrays and objects while removing falsy values. Each element is processed once, and nested structures are compacted recursively to rebuild the final object or array.
Recursion allows you to process nested arrays and objects in a natural way. Each recursive call handles a smaller part of the structure, making it easier to compact deeply nested values without complex iterative logic.
Problems like Compact Object appear in JavaScript-focused interviews because they test recursion, object manipulation, and understanding of falsy values. Variations of deep traversal problems are common in frontend and full‑stack roles.
Understanding arrays, objects (hash maps), and recursion is key. You must also be comfortable checking value truthiness and reconstructing nested data structures while traversing them.
This Python implementation uses an explicit stack to perform an iterative deep-first traversal on the JSON structure. By handling keys/indices dynamically, the solution effectively compacts nested constructs without recursive function calls.