How to remove duplicate elements form list in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();

            list.Add(1);
            list.Add(2);
            list.Add(3);
            list.Add(3);
            list.Add(3);
            list.Add(5);
            list.Add(5);
            list.Add(6);

            List<int> distinct_list = list.Distinct().ToList();

            foreach (int element in distinct_list) {
                Console.WriteLine(element);
            }
        }
    }
}


/*
run:
  
1
2
3
5
6
   
*/

 



answered Aug 26, 2018 by avibootz

Related questions

1 answer 99 views
1 answer 157 views
2 answers 212 views
1 answer 173 views
2 answers 128 views
1 answer 104 views
1 answer 100 views
...