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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:09:40+00:00 2026-05-31T01:09:40+00:00

I am current working on a project where I need to generate all possible

  • 0

I am current working on a project where I need to generate all possible permutations from a given set of characters. I am currently using this code:

public static IEnumerable<string> AllPermutations(this IEnumerable<char> s)
{
    return s.SelectMany(x =>
    {
        var index = Array.IndexOf(s.ToArray(), x);
        return s.Where((y, i) => i != index).AllPermutations().Select(y => new string(new[] { x }.Concat(y).ToArray())).Union(new[] { new string(new[] { x }) });
    }).Distinct();
}

From this answer.

The problem I have is that it won’t generate permuations that use the same letter more than once.

For example if I used abcde as the input I need it to generate combinations like aaaaa and dcc etc.

I’m not experienced enough with LINQ to understand where the code is stopping duplicate letters. Any help is greatly appreciated.

  • 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-31T01:09:41+00:00Added an answer on May 31, 2026 at 1:09 am

    This might work, but I’m sure it could be done more efficiently (taking the counting prompt from PeskyGnat):

        static IEnumerable<string> GetVariations(string s)
        {
            int[] indexes = new int[s.Length];
            StringBuilder sb = new StringBuilder();
    
            while (IncrementIndexes(indexes, s.Length))
            {
                sb.Clear();
                for (int i = 0; i < indexes.Length; i++)
                {
                    if (indexes[i] != 0)
                    {
                        sb.Append(s[indexes[i]-1]);
                    }
                }
                yield return sb.ToString();
            }
        }
    
        static bool IncrementIndexes(int[] indexes, int limit)
        {
            for (int i = 0; i < indexes.Length; i++)
            {
                indexes[i]++;
                if (indexes[i] > limit)
                {
                    indexes[i] = 1;
                }
                else
                {
                    return true;
                }
            }
            return false;
        }
    

    Edit: Changed to use yield return as per Rawlings suggestion. Much better memory usage if you don’t need to keep all the results and you can start using the results before they’ve all been generated.

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

Sidebar

Related Questions

For a current project I am working I need to return an aggregate report
I reference another eclipse project from the current project I am working on. Everything
How can I retrieve the current working directory of cmd.exe? This seems possible. For
I'm currently working on a project where I need to print out a lesson
I'm working on a project that is using code from an OpenSource project. One
I am working on project where I need to delete all empty folder (directories)
I am working on a project where I need to solve a maze using
I am working on a project in which I need to know the current
Currently working on a project where we need to drop in various static html
I am current working on a project where, as different users add text to

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.