Remove duplicates from a list preserving order using HashSet.
Ready — edit the code above and click Run.
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.
HashSet.Add returns false if item exists—efficient dedupe.