Technical skill practice

Remove Duplicates from a JavaScript Array: Choose the Identity Rule

Duplicate removal needs a definition of “same.” Set can remove repeated primitive IDs, but two object records with equal-looking fields remain distinct references. For records, state which key determines duplication and which occurrence should win.

The task

Keep the first record for each id and preserve arrival order. Compare that policy with direct Set construction on the same objects.

Runnable Node 22 example

const assert = require('node:assert/strict');

assert.deepEqual([...new Set(['A', 'B', 'A'])], ['A', 'B']);
const rows = [{id: 'A', value: 1}, {id: 'A', value: 2}, {id: 'B', value: 3}];
assert.equal(new Set(rows).size, 3);
const seen = new Set();
const unique = rows.filter(row => {
  if (seen.has(row.id)) return false;
  seen.add(row.id);
  return true;
});
assert.deepEqual(unique.map(row => row.value), [1, 3]);
assert.equal(unique[0], rows[0]);
console.log(JSON.stringify(unique));

Expected output and explanation

The primitive example reduces three labels to two. Directly placing the three records into a Set retains all three because they are separate objects. The explicit seen-key step instead treats the repeated A key as a duplicate.

The retained value for A is one because this algorithm keeps the first occurrence. Keeping the last would be a different policy with potentially different ordering. The result also shares the retained objects rather than cloning their fields.

Output from the example

[{"id":"A","value":1},{"id":"B","value":3}]

Pitfall to check

Choose a valid, stable key before deduplication. If ids may be missing, this algorithm could treat multiple missing keys as one group. Decide whether that is an error instead of quietly discarding data.

Questions, answered

Can JSON.stringify(record) define duplicate records?

Only under a deliberately specified serialization-based rule, with its limitations understood. A domain key is often clearer than relying on text serialization for identity.

Sources and examples

ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.

Your experience. A complete resume.

Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.

Free account required. Free-plan PDF exports include a watermark.