首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

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

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

热点排行