$ ./guess_the_languageProblem Solving — fix the bug
Problem Solving — fix the bug · hard · #47

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);
  });
}
  1. Change Set<T> to Array<T> in the type
  2. Change filter to map in the pipeline
  3. Add x to seen twice inside the filter
  4. Return !seen.has(x) BEFORE calling seen.add(x)
Adding before checking makes has always true; check first.
Play Problem Solving ▸