Easy csharp

Collections — List remove duplicates

Problem

Remove duplicates from a list preserving order using HashSet.

Hints
  • HashSet.Add returns bool

Your practice code

Ready — edit the code above and click Run.

Solution

using System;
using System.Collections.Generic;

class Program {
    static void Main() {
        var input = new List<int> { 1, 2, 2, 3, 1, 4 };
        var seen = new HashSet<int>();
        var unique = new List<int>();
        foreach (var x in input)
            if (seen.Add(x)) unique.Add(x);
        Console.WriteLine(string.Join(",", unique));
    }
}

Try solving on your own first, then reveal the official answer.

Explanation

HashSet.Add returns false if item exists—efficient dedupe.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details