Create Object from Two Arrays - Problem

You are given two arrays: keysArr containing potential object keys and valuesArr containing corresponding values. Your task is to merge these arrays into a single object where each key-value pair comes from keysArr[i] and valuesArr[i].

Key Rules:

  • Duplicate Prevention: If a key appears multiple times, only the first occurrence should be included in the final object
  • Type Conversion: All keys must be converted to strings using String()
  • Index Matching: keysArr[i] pairs with valuesArr[i]

This problem simulates creating a configuration object from separate key and value arrays, commonly seen in data processing and API responses.

Input & Output

example_1.py โ€” Basic Case
$ Input: keysArr = ["a", "b", "c"], valuesArr = [1, 2, 3]
โ€บ Output: {"a": 1, "b": 2, "c": 3}
๐Ÿ’ก Note: No duplicate keys, so all key-value pairs are included in the result object.
example_2.py โ€” Duplicate Keys
$ Input: keysArr = ["a", "b", "a", "c"], valuesArr = [1, 2, 3, 4]
โ€บ Output: {"a": 1, "b": 2, "c": 4}
๐Ÿ’ก Note: The key "a" appears at indices 0 and 2. Only the first occurrence (index 0) with value 1 is kept, the duplicate at index 2 with value 3 is ignored.
example_3.py โ€” Type Conversion
$ Input: keysArr = [1, "1", 2], valuesArr = ["first", "second", "third"]
โ€บ Output: {"1": "first", "2": "third"}
๐Ÿ’ก Note: The number 1 and string "1" both convert to the same string key "1". Only the first occurrence (number 1) is kept with value "first".

Constraints

  • 0 โ‰ค keysArr.length = valuesArr.length โ‰ค 104
  • keysArr[i] can be any type that can be converted to string
  • valuesArr[i] can be any valid JavaScript value
  • Both arrays will always have the same length

Visualization

Tap to expand
Creating Object from Two ArraysInput Arrays"a""b""a""c"Keys1234ValuesProcessing with SetSeen Keys Set{"a", "b", "c"}O(1) lookupResult Object{"a": 1, "b": 2, "c": 4}Step-by-Step Process1. Process ("a", 1): "a" not in Set โ†’ Add to both Set and result2. Process ("b", 2): "b" not in Set โ†’ Add to both Set and result3. Process ("a", 3): "a" already in Set โ†’ Skip (duplicate)4. Process ("c", 4): "c" not in Set โ†’ Add to both Set and resultFinal Result: {"a": 1, "b": 2, "c": 4}Time Complexity: O(n) | Space Complexity: O(n)
Understanding the Visualization
1
Initialize Tracking
Start with empty result object and Set to track processed keys
2
Process Each Pair
Convert key to string and check if we've seen it before
3
Add New Keys
If key is new, add to both tracking Set and result object
4
Skip Duplicates
If key already exists in Set, ignore this occurrence
Key Takeaway
๐ŸŽฏ Key Insight: Using a Hash Set for O(1) duplicate detection transforms an O(nยฒ) nested loop problem into an optimal O(n) single-pass solution.
Asked in
Google 35 Amazon 28 Meta 22 Microsoft 18
23.4K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen