Which fix removes the bug?
function uniq<T>(items: T[]): T[] {
const seen = new Set<T>();
return items.filter((x) => {
seen.add(x);
return !seen.has(x);
});
}
- Change
Set<T>toArray<T>in the type - Change
filtertomapin the pipeline - Add
xto seen twice inside the filter - Return
!seen.has(x)BEFORE callingseen.add(x)
Adding before checking makes
Play Problem Solving ▸
has always true; check first.