How can I get elements uniquely from an array

how can i get elements uniquely from an array

0


a source to share


3 answers


Using LINQ can easily solve your problem:

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

      

+1


a source


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

      



+4


a source


GenericList.Distinct()

      

0


a source







All Articles