List<int> listWithDuplicates = new List<int>() { 1, 2, 3, 4, 1, 2, 3, 4 };
listWithDuplicates.Distinct().ToList();
listWithDuplicates.GroupBy(x => x).Select(d => d.First()).ToList();
listWithDuplicates.Union(listWithDuplicates).ToList();
listWithDuplicates.ToHashSet().ToList();
new HashSet<T>(listWithDuplicates).ToList();
var dict = new Dictionary<T, int>(); foreach (var s in listWithDuplicates) { dict.TryAdd(s, 1); } var distinctList = dict.Keys.ToList();
var listWithoutDuplicates = new List<T>(); foreach (T item in listWithDuplicates) { if (!listWithoutDuplicates.Contains(item)) //we can also use !listWithoutDuplicates.Any(x => x.Equals(item)) { listWithoutDuplicates.Add(item); } }
var n = listWithDuplicates.Count; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (listWithDuplicates.ElementAt(i).Equals(listWithDuplicates.ElementAt(j))) { for (int k = j; k < n - 1; k++) { T item = listWithDuplicates.ElementAt(k); item = listWithDuplicates.ElementAt(k + 1); } j--; n--; } } } var distinctList = listWithDuplicates.Take(n).ToList();
var size = listWithDuplicates.Count; for (int i = 0; i < size; i++) { for (int j = i + 1; j < size; j++) { if (listWithDuplicates.ElementAt(i)!.Equals(listWithDuplicates.ElementAt(j))) { size--; T jThItem = listWithDuplicates.ElementAt(j); jThItem = listWithDuplicates.ElementAt(size); j--; } } } listWithDuplicates.Take(size).ToList();
public List<T> UsingRecursion(List<T>? listWithDuplicates, List<T>? listWithoutDuplicates = default, int index = 0) { if (listWithoutDuplicates == null) { listWithoutDuplicates = new List<T>(); } if (index >= listWithDuplicates.Count) { return listWithDuplicates; } if (listWithoutDuplicates.IndexOf(listWithDuplicates[index]) < 0) { listWithoutDuplicates.Add(listWithDuplicates[index]); } UsingRecursion(listWithDuplicates, listWithoutDuplicates, index + 1); return listWithoutDuplicates.ToList(); }
var listWithoutDuplicates = new List<T>(); listWithDuplicates = listWithDuplicates.OrderBy(x => x).ToList(); T element = default; foreach (T result in listWithDuplicates) { if (!result.Equals(element)) { listWithoutDuplicates.Add(result); element = result; } }
| Method | Mean | Error | StdDev | Rank | Gen 0 | Allocated | |---------------------------- |-----------:|----------:|----------:|-----:|-------:|----------:| | EmptyListWithContainsMethod | 1.213 us | 0.0115 us | 0.0107 us | 1 | 0.0172 | 72 B | | DictionaryMethod | 1.338 us | 0.0213 us | 0.0189 us | 2 | 0.0687 | 288 B | | ConvertToHashSetMethod | 2.409 us | 0.0482 us | 0.0610 us | 3 | 0.9918 | 4,160 B | | DistinctLINQMethod | 2.424 us | 0.0431 us | 0.0382 us | 3 | 1.0071 | 4,224 B | | InitializingHashSetMethod | 2.446 us | 0.0477 us | 0.0823 us | 3 | 0.9918 | 4,160 B | | UnionLINQMethod | 3.981 us | 0.0401 us | 0.0313 us | 4 | 0.0916 | 392 B | | GroupByLINQMethod | 4.653 us | 0.0428 us | 0.0526 us | 5 | 0.8698 | 3,664 B | | RecursiveMethod | 7.302 us | 0.1636 us | 0.4507 us | 6 | 3.4561 | 14,472 B | | SortMethod | 7.756 us | 0.1248 us | 0.1622 us | 7 | 1.9989 | 8,376 B | | IterationsAndSwappingMethod | 9.103 us | 0.1149 us | 0.1018 us | 8 | 1.1597 | 4,888 B | | EmptyListWithAnyMethod | 11.466 us | 0.2288 us | 0.5022 us | 9 | 8.5297 | 35,680 B | | IterationsAndShiftingMethod | 389.509 us | 5.3991 us | 6.2176 us | 10 | 0.9766 | 4,888 B |