如何从数组中获取唯一的元素
答案 0 :(得分:4)
using System.Linq;
class Program
{
static void Main()
{
var array = new int[] { 1, 2, 2, 3 };
var distinctArray = array.Distinct().ToArray();
}
}
答案 1 :(得分:1)
使用LINQ可以轻松解决您的问题:
class Program
{
static string[] ar = new[] { "a", "b", "c", "d", "a", "a", "f", "g",
"d", "i", "j", "a","d", "c", "g" };
static void Main(string[] args)
{
var dist = (from a in ar select a).Distinct();// distinct;
foreach (var v in dist)
Console.Write(v);
Console.ReadLine();
}
}
它产生这个输出:
abcdfgij
答案 2 :(得分:0)
GenericList.Distinct()