Drill

ProblemsTypeScript › data

Drop the repeats

easydataTypeScript

A feed sometimes sends the same id twice, and a clean list keeps only the first sighting.

dedupe(values: list<int>) → list<int>

Solve it in the editor →

Where you start

function dedupe(values: number[]): number[] {
  
}

Worked examples

CallResult
dedupe([1,2,3,1,2])[1,2,3]
dedupe([1,1,1])[1]
dedupe([])[]
dedupe([3,1,2,1,3])[3,1,2]

Hint

Carry a set of what you have already seen, and only push a value the first time you meet it.

Reference solution in TypeScript
function dedupe(values: number[]): number[] {
  const seen = new Set<number>();
  const result: number[] = [];
  for (const v of values) {
    if (!seen.has(v)) {
      seen.add(v);
      result.push(v);
    }
  }
  return result;
}

The same problem in another language

More data problems in TypeScript