Click here to Skip to main content
15,900,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a list as follows
{dog,dog,cat,cat,mouse,mouse,cat,cat}
i want to remove the repeating values as well as rearrange the list in the order
{dog,mouse,cat} since cat gets added to the list in the end
How do i do it?
Posted

1 solution

C#
mylist = mylist.Distinct().ToList();

will remove duplicates and assign back to your list
if you need to keep the item based on last insert index, then
C#
List<string> list = new List<string>{"dog","dog","cat","cat","mouse","mouse","cat","cat"};
list = list.Select((x,index)=> new {Val=x, Index=index}).GroupBy(y=>y.Val)
        .Select(g=>new {g.Key, maxin= g.Max(x=>x.Index)}).OrderBy(x=>x.maxin).Select(x=>x.Key).ToList();
 
Share this answer
 
v2

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