If I have a dictionary like so,
Dictionary<int, string> roadNames = new Dictionary<int, string>();
roadNames.Add(1, "Rosedale Rd");
roadNames.Add(2, "Transmere Rd");
roadNames.Add(3, "Rosedale Rd");
roadNames.Add(4, "Rosedale Rd");
roadNames.Add(5, "Rosedale Rd");
roadNames.Add(6, "Rosedale Rd");
roadNames.Add(7, "Rosedale Rd");
roadNames.Add(8, "Brown Rd");
roadNames.Add(9, "Harold Rd");
Is there a LINQ solution to remove the duplicates that are NEXT to each other. The result I am after is a list containing this,
Rosedale Rd
Transmere Rd
Rosedale Rd
Brown Rd
Harold Rd
Note that Rosedale Rd is still in the list twice. The idea is to remove duplicates that are next to each other, and in this case we are removing item 4, 5, 6, and 7.
Items 1 is not next to item 3, so it isn’t removed.
UPDATE:
Don’t worry about Dictionary not being ordered. Solutions for a list that is in order would be fine. I can handle the ordering. i.e.
List<string> roadNames = new List<string>()
{
"Rosedale Rd",
"Transmere Rd",
// etc
};
Assuming you’re using a sorted dictionary instead (or any other sorted structure), there are two options.
Leverage Reactive Extensions
This is very simple if you leverage Reactive Extensions from Microsoft (which everyone should!):
You can change that final
ToList()to toToEnumerable()instead if you like.This returns:
Use an Extension Method
You can use a
GroupAdjacentextension method as such:The extension method: