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;- ✓Correct answer: A
Setcompares objects by reference, so two different objects both stay (size 2) - 2A
Setcan't store objects at all - 3A
Setcompares objects withJSON.stringifyand keeps one (size 1)
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])).