Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };
List<int> New = new List<int>();
foreach (int item in li)
    New.Add(item);
Posted

Hi..

Try below code of line to have distinct values in your new list called distinct..

C#
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };// your original list containing duplicates</int></int>


C#
List<int> distinct = li.Distinct().ToList();// new list containing only distinct values from above list..</int>


Hope it works fine..
 
Share this answer
 
v2
Try a HashSet instead (unless order is significant).
 
Share this answer
 
XML
List<int> li = new List<int>() { 1, 1, 1, 1, 1, 1, 1, 2, 2 };
           List<int> New = new List<int>();
           foreach(int i in li)
           {
               if (!New.Contains(i))
                   New.Add(i);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900