我在 C# 中有一个整数列表。我希望删除重复项。在 C++ 中,我将通过 std::sort 和 std::unique 算法运行它,以非常有效地获取唯一列表。
在 C# 中做同样事情的最佳方法是什么?换句话说,我正在寻找一种更优雅的方式来执行以下代码:
private static int[] unique(int[] ids)
{
IDictionary<int, object> d = new Dictionary<int, object>();
foreach(int i in ids)
d[i] = null;
int[] results = new int[d.Count];
int j = 0;
foreach(int id in d.Keys)
results[j++] = id;
return results;
}