Watch 10 video solutions for Create Object from Two Arrays, a easy level problem. This walkthrough by Greg Hogg has 627,264 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given two arrays keysArr and valuesArr, return a new object obj. Each key-value pair in obj should come from keysArr[i] and valuesArr[i].
If a duplicate key exists at a previous index, that key-value should be excluded. In other words, only the first key should be added to the object.
If the key is not a string, it should be converted into a string by calling String() on it.
Example 1:
Input: keysArr = ["a", "b", "c"], valuesArr = [1, 2, 3]
Output: {"a": 1, "b": 2, "c": 3}
Explanation: The keys "a", "b", and "c" are paired with the values 1, 2, and 3 respectively.
Example 2:
Input: keysArr = ["1", 1, false], valuesArr = [4, 5, 6]
Output: {"1": 4, "false": 6}
Explanation: First, all the elements in keysArr are converted into strings. We can see there are two occurrences of "1". The value associated with the first occurrence of "1" is used: 4.
Example 3:
Input: keysArr = [], valuesArr = []
Output: {}
Explanation: There are no keys so an empty object is returned.
Constraints:
keysArr and valuesArr are valid JSON arrays2 <= JSON.stringify(keysArr).length, JSON.stringify(valuesArr).length <= 5 * 105keysArr.length === valuesArr.lengthProblem Overview: You receive two arrays: one containing keys and the other containing values. The task is to create an object where keys[i] maps to values[i]. If the same key appears multiple times, the later value should overwrite the previous one.
Approach 1: Nested Scan for Duplicate Handling (O(n^2) time, O(n) space)
A straightforward but inefficient strategy is to iterate through the keys array and, for each element, scan earlier elements to check if the key already appeared. If it did, update the stored value; otherwise insert a new key-value pair into the object. This method works but repeatedly scanning the array causes quadratic time complexity. It’s mainly useful as a conceptual baseline when first reasoning about duplicate keys.
Approach 2: Single Pass Object / Hash Map (O(n) time, O(n) space)
The optimal approach uses a single iteration and stores results directly in an object (effectively a hash map). Iterate from index 0 to n - 1, assigning result[keys[i]] = values[i]. JavaScript objects perform average O(1) insertion and overwrite operations, so duplicates are naturally handled by replacing the previous value. This keeps the algorithm linear and avoids unnecessary lookups. The technique is a common pattern when combining parallel arrays and relies on constant‑time hash access from a hash table.
Approach 3: Functional Reduce Construction (O(n) time, O(n) space)
Another clean implementation uses Array.reduce(). The reducer accumulates an object while iterating through the keys array, assigning acc[key] = values[index] at each step. Internally this still performs one pass and the same constant-time assignments as the hash-map solution. The benefit is concise functional code, though the underlying algorithmic behavior remains identical to the iterative approach.
Both optimal approaches rely on basic array traversal and object property assignment, which acts as a hash lookup structure in JavaScript. The overwrite behavior ensures the final object reflects the last occurrence of each key.
Recommended for interviews: Use the single-pass hash map approach. Interviewers expect you to recognize that a direct mapping from the two arrays avoids unnecessary checks and runs in O(n) time. Mentioning the naive duplicate-checking method demonstrates reasoning from brute force to optimal, but implementing the linear solution shows strong understanding of arrays and hash-based data structures.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Nested Scan for Duplicate Keys | O(n^2) | O(n) | Conceptual baseline or when exploring duplicate detection manually |
| Single Pass Hash Map / Object | O(n) | O(n) | General case and the expected optimal interview solution |
| Functional Reduce Construction | O(n) | O(n) | When writing concise functional JavaScript or TypeScript code |