日期:2014-05-17  浏览次数:20764 次

c#中去除数组中重复的数据
有一个数组比如int a[5]={0,1,2,1,4},如何变为int a[4]={0,1,2,4},请高手回答。。。

------解决方案--------------------
int[] a = { 0, 1, 2, 1, 4 };
int[] result = a.Distinct().ToArray();
------解决方案--------------------
非Linq方法
C# code

int[] a = { 0, 1, 2, 1, 4 };
List<int> temp = new List<int>();
foreach (int i in a)
{
    if (temp.Contains(i)) continue;
    temp.Add(i);
}
int[] result = temp.ToArray();