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 1045691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:01:31+00:00 2026-05-16T16:01:31+00:00

I am trying to remove elements from a Dictionary<string, List<string>> in C# when the

  • 0

I am trying to remove elements from a Dictionary<string, List<string>> in C# when the count of the list<string> is lesser than or equal to 1.
I got some code working but it is not elegant and I have a gut feeling that this can be done elegantly in linq.

This is the code I have now

        Dictionary<string,List<string>> FindAnagrams(List<string> dictionary)
        {
            Dictionary<string, List<string>> anagrams = new Dictionary<string, List<string>>();
            foreach (string word in dictionary)
            {
                char[] charArray=word.ToCharArray();
                Array.Sort(charArray);
                string sorted=new string(charArray);
                if (anagrams.ContainsKey(sorted))
                    anagrams[sorted].Add(word);
                else
                    anagrams.Add(sorted, new List<string>() { word });
            }
            List<string> nonAnagrams = new List<string>();
            foreach (var sorted in anagrams.Keys)
                if (anagrams[sorted].Count == 1)
                    nonAnagrams.Add(sorted);
            foreach(string word in nonAnagrams)
                anagrams.Remove(word);               
            return anagrams;
        }

Below is how far I got using linq but this ain’t working.

var realAna = from keys in anagrams.Keys
              where anagrams[keys].Count >1
              select anagrams.values;

To put the problem in context I am trying to find anagrams from a dictionary, I consider a words as having anagrams if the sorted key has more than one value associated with it.

  • 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-05-16T16:01:32+00:00Added an answer on May 16, 2026 at 4:01 pm
    var anagrams = new Dictionary<string, IList<string>>()
    {
     {"hello", new List<string>(){"hello", "helol", "hlelo"}},
     {"hi", new List<string>(){"hi"}},
     {"me", new List<string>(){"me", "em"}}
    };
    
    var a2 = anagrams
     .Where(x => x.Value.Count > 1)
     .Aggregate(new Dictionary<string, IList<string>>(),
      (acc, item) => { acc.Add(item.Key, item.Value); return acc; });
    

    This uses non-query form linq, and is built up programatically.

    • The Where Selects all key/value pairs in the dictionary where the list has more than one item.
    • The Select I removed because it’s actually not needed anymore. 🙂
    • The Aggregate collects the pairs and performs an add for each item (adding it into the list). You could also use .ToDictionary(...) here.

    If you need to sort your sub-lists change item.Value to item.Value.Sort(s => s).ToList()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to remove elements from a std::list and keep some stats of
I am trying to remove all HTML elements from a String. Unfortunately, I cannot
I am trying to add and remove elements from a list mapped as .HasMany(),
I am trying to remove comments from a list of elements that were obtained
In this multi-thread program I'm trying to remove two random elements from a list
I'm trying to remove event listeners from elements after they've been clicked on and
I'm trying to load page and then remove some elements in it. While doing
Let's say I am trying to remove elements from array a = [1,1,1,2,2,3] .
I am trying to remove all script elements from a HTML page. But for
I'm trying to remove elements from webpages that aren't essential using querySelectorAll , but

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.