我怎样才能从数组中获得唯一的元素

时间:2021-09-18 13:45:12

how can i get elements uniquely from an array

我怎样才能从数组中获得唯一的元素

3 个解决方案

#1


Using LINQ can solve your problem easily:

使用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();
    }
}

It produces this output:

它产生这个输出:

abcdfgij

#2


using System.Linq;
class Program
{
   static void Main()
   {
      var array = new int[] { 1, 2, 2, 3 };
      var distinctArray = array.Distinct().ToArray();
   }
}

#3


GenericList.Distinct()

#1


Using LINQ can solve your problem easily:

使用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();
    }
}

It produces this output:

它产生这个输出:

abcdfgij

#2


using System.Linq;
class Program
{
   static void Main()
   {
      var array = new int[] { 1, 2, 2, 3 };
      var distinctArray = array.Distinct().ToArray();
   }
}

#3


GenericList.Distinct()