Unique Items by Key
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Write uniqueBy(items, keyFn): return a new array holding only the first item for each key, in their original order, where an item's key is keyFn(item). Keys can be any value, and are compared the way a Set compares them: triple equals for most values, so objects match only when they're the same object, except that NaN matches NaN. Don't change items. The checks include 200,000 items, so comparing each item with every one kept so far is too slow.
Your code must pass
- Keeps the first item for each key, in order
- Compares keys like a Set, including objects and NaN
- Returns a new array and leaves the input alone
- Is fast enough for 200,000 items