Im looking for another solution with LINQ preferable for:
List<int> distinctAges = new List<int>();
for (int indexInt = 0; indexInt < ages.Count; indexInt++)
if (!distinctAges.Contains(ages[indexInt]))
distinctAges.Add(ages[indexInt]);
// 69.98 ms
vs
List<int> distinctAges = new List<int>();
foreach(int singleAge in ages)
if (!distinctAges.Contains(singleAge))
distinctAges.Add(singleAge);
// 94.89 ms
another solution:
List<int> distinctAges = ages.GroupBy(singleNumber => singleNumber).Select(singleGroup => singleGroup.Key).ToList();
// 293.22 ms
or:
List<int> distinctAges = ages.Distinct().ToList();
// 103.16 ms
Shown time is from higher loop and result must to be in the List. I'm searching for a solution, which is not done by for/foreach and execution time is similar to for/foreach. Any idea about it ?