Algorithms and Data Structures · Junior · Question 74 of 75

Why is a Set of objects not useful for removing duplicate objects with the same content?

new Set([{ id: 1 }, { id: 1 }]).size;

Explanation

Set uses SameValueZero, which for objects means identity. To dedupe by content, key a Map by an identifying field: new Map(items.map((x) => [x.id, x])).

Further reading

Problem with a question

Algorithms and Data Structures · Junior · question 74

Test yourself

Take the Algorithms and Data Structures · Junior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz