【发布时间】:2021-03-26 03:16:33
【问题描述】:
这是 Distinct from https://docs.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct 的用法示例
List<int> ages = new List<int> { 21, 46, 46, 55, 17, 21, 55, 55 };
IEnumerable<int> distinctAges = ages.Distinct();
Console.WriteLine("Distinct ages:");
foreach (int age in distinctAges)
{
Console.WriteLine(age);
}
/*
This code produces the following output:
Distinct ages:
21
46
55
17
*/
算法似乎很简单:迭代元素。如果当前元素未知,则将其复制到结果中,否则将其丢弃。
我想知道是否所有实现都需要给出这个结果,或者未来的实现是否也能输出 55、21、17、46?
【问题讨论】:
-
虽然不能保证保持相同的顺序,但我非常怀疑实现会改变以导致不同的顺序。