Create Object from Two Arrays - Problem
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].
Important rules:
- If a duplicate key exists at a previous index, that key-value should be excluded. Only the first occurrence of each key should be added to the object.
- If the key is not a string, it should be converted to a string by calling
String()on it.
Input & Output
Example 1 — Basic Case
$
Input:
keysArr = ["a", "b", "c"], valuesArr = [1, 2, 3]
›
Output:
{"a":1,"b":2,"c":3}
💡 Note:
All keys are unique strings, so all key-value pairs are added: a→1, b→2, c→3
Example 2 — Duplicate Keys
$
Input:
keysArr = ["a", "b", "a"], valuesArr = [1, 2, 3]
›
Output:
{"a":1,"b":2}
💡 Note:
Key 'a' appears at indices 0 and 2. Only the first occurrence (a→1) is kept, the second is excluded
Example 3 — Non-String Keys
$
Input:
keysArr = [1, 2, 1], valuesArr = ["x", "y", "z"]
›
Output:
{"1":"x","2":"y"}
💡 Note:
Numbers are converted to strings: 1→"1", 2→"2". The duplicate key "1" at index 2 is excluded
Constraints
- 1 ≤ keysArr.length = valuesArr.length ≤ 50
- 1 ≤ keysArr[i].length ≤ 10
- keysArr[i] is made of alphanumeric characters
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code