Drill

ProblemsC# › data

Drop the repeats

easydataHash mapsArraysC#

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

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

C# needs a compiler and Drill does not host one yet, so this page is the reference rather than an exercise: the problem, worked examples, and the solution in full. To type it out, the same problem runs in Python.

Solve it in Python →

Where you start

public List<int> Dedupe(List<int> values) {
    
}

Worked examples

CallResult
Dedupe(new List<int> { 1, 2, 3, 1, 2 })new List<int> { 1, 2, 3 }
Dedupe(new List<int> { 1, 1, 1 })new List<int> { 1 }
Dedupe(new List<int> { })new List<int> { }
Dedupe(new List<int> { 3, 1, 2, 1, 3 })new List<int> { 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 C#
public List<int> Dedupe(List<int> values) {
    var seen = new HashSet<int>();
    var result = new List<int>();
    foreach (var v in values) {
        if (seen.Add(v)) result.Add(v);
    }
    return result;
}

The same problem in another language

More data problems in C#