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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:47:24+00:00 2026-05-15T21:47:24+00:00

Is it possible to generate all permutations of a collection in c#? char[] inputSet

  • 0

Is it possible to generate all permutations of a collection in c#?

char[] inputSet = { 'A','B','C' };
Permutations<char> permutations = new Permutations<char>(inputSet);
foreach (IList<char> p in permutations)
{
   Console.WriteLine(String.Format("{{{0} {1} {2}}}", p[0], p[1], p[2]));
}
  • 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-15T21:47:25+00:00Added an answer on May 15, 2026 at 9:47 pm

    I’ve already faced the problem and I wrote these simple methods:

        public static IList<T[]> GeneratePermutations<T>(T[] objs, long? limit)
        {
            var result = new List<T[]>();
            long n = Factorial(objs.Length);
            n = (!limit.HasValue || limit.Value > n) ? n : (limit.Value);
    
            for (long k = 0; k < n; k++)
            {
                T[] kperm = GenerateKthPermutation<T>(k, objs);
                result.Add(kperm);
            }
    
            return result;
        }
    
        public static T[] GenerateKthPermutation<T>(long k, T[] objs)
        {
            T[] permutedObjs = new T[objs.Length];
    
            for (int i = 0; i < objs.Length; i++)
            {
                permutedObjs[i] = objs[i];
            }
            for (int j = 2; j < objs.Length + 1; j++)
            {
                k = k / (j - 1);                      // integer division cuts off the remainder
                long i1 = (k % j);
                long i2 = j - 1;
                if (i1 != i2)
                {
                    T tmpObj1 = permutedObjs[i1];
                    T tmpObj2 = permutedObjs[i2];
                    permutedObjs[i1] = tmpObj2;
                    permutedObjs[i2] = tmpObj1;
                }
            }
            return permutedObjs;
        }
    
        public static long Factorial(int n)
        {
            if (n < 0) { throw new Exception("Unaccepted input for factorial"); }    //error result - undefined
            if (n > 256) { throw new Exception("Input too big for factorial"); }  //error result - input is too big
    
            if (n == 0) { return 1; }
    
            // Calculate the factorial iteratively rather than recursively:
    
            long tempResult = 1;
            for (int i = 1; i <= n; i++)
            {
                tempResult *= i;
            }
            return tempResult;
        }
    

    Usage:

    var perms = Utilities.GeneratePermutations<char>(new char[]{'A','B','C'}, null);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to generate all permutations of a string in PHP? I want
Possible Duplicate: Generate list of all possible permutations of a string I need to
I would like to generate a list of all possible permutations of a string
Possible Duplicate: How to generate all permutations of a list in Python I want
Algorithm to generate all possible letter combinations of given string down to 2 letters
I want to generate all possible permutations for specific consonant/vowel setups. For example, I
How do I generate all possible permutations of a list of numbers in C?
I have a list of words and I need to generate all possible permutations
I am trying to work out the best way to generate all possible permutations
How can I generate the shortest sequence with contains all possible permutations? Example: For

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.