Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7974215
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:18:30+00:00 2026-06-04T08:18:30+00:00

If I have a dictionary like so, Dictionary<int, string> roadNames = new Dictionary<int, string>();

  • 0

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
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T08:18:32+00:00Added an answer on June 4, 2026 at 8:18 am

    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!):

    roadNames.Values // remove if a list instead
             .ToObservable()
             .DistinctUntilChanged()
             .ToList();
    

    You can change that final ToList() to to ToEnumerable() instead if you like.

    This returns:

    Rosedale Rd 
    Transmere Rd 
    Rosedale Rd 
    Brown Rd 
    Harold Rd 
    

    Use an Extension Method

    You can use a GroupAdjacent extension method as such:

    roadNames.Values // remove if a list instead
             .GroupAdjacent((x,y) => x == y)
             .Select(x => x.First());
    

    The extension method:

    public static IEnumerable<IEnumerable<T>> GroupAdjacent<T>(
        this IEnumerable<T> source, Func<T, T, bool> adjacent)
    {
        var g = new List<T>();
        foreach (var x in source)
        {
            if (g.Count != 0 && !adjacent(g.Last(), x))
            {
                yield return g;
                g = new List<T>();
            }
            g.Add(x);
        }
        yield return g;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I Have a Challenge like this, Dictionary<string,Dictionary<string,int>> dic=new Dictionary<string,Dictionary<string,int>>(); i want them into a
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Say I have a data structure like a Dictionary<string, Dictionary<string, int>> or similar, and
I have two dictionaries with the same structure: Dictionary<string, int> foo = new Dictionary<string,
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
When I have a Dictionary<string, int> actual and then create a completely new Dictionary<string,
I have a dictionary like the follow: public Dictionary<int, SpawnList> spawnEntities = new Dictionary<int,
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I have a Dictionary that has a signature: Dictionary<int, List<string>> . I'd like to
I have a List of String like List<String> MyList=new List<String>{A,B}; and a Dictionary<String, Dictionary<String,String>>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.