Skip to content
RED SAGSkills

Dedupe Records by Key

An import job receives customer records (plain objects) that may contain duplicates. Two records are duplicates when they have the same value under key.

Return the records with duplicates removed, keeping the first occurrence of each value and preserving the original order. Values are compared with strict equality, so the number 1 and the string "1" are different values.

Records where key is missing or null cannot be deduplicated, so they are always kept.

Example 1

Input: records = [{"id":1,"name":"Asha"},{"id":2,"name":"Ben"},{"id":1,"name":"Asha K"}], key = "id"
Output: [{"id":1,"name":"Asha"},{"id":2,"name":"Ben"}]
Explanation: The second record with id 1 is dropped.

Example 2

Input: records = [{"email":"a@x.io"},{"email":"b@x.io"},{"email":"a@x.io"},{"email":"b@x.io"}], key = "email"
Output: [{"email":"a@x.io"},{"email":"b@x.io"}]

Constraints

  • 0 <= records.length <= 10^5
  • Key values are strings, numbers or booleans when present
Hint 1

A Set of values you have already seen gives O(1) lookups.

Hint 2

Make sure 1 and "1" do not collide, for example by prefixing the type.

Good practice for: Backend Developer, Full Stack Developer, Data Engineer, Database Administrator, Frontend Developer, Automation Test Engineer

Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.