读书人

c#中除开数组中重复的数据

发布时间: 2012-09-29 10:30:01 作者: rapoo

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(); 

读书人网 >C#

热点推荐