3

How do I go about getting a combination of an array of strings of count 2? Ie.

List<string> myString = {"a", "b", "c", "d", "f"};

A permutation would look like this:

ab ac ad af ba bc bd bf ca cb cd cf etc...

I have no idea how to begin this algorithm. If it helps, I'd rather do a loop than a recursion because in my actual implementation, I have to assign a value to the permuted items and compare each one to another and select the highest.

4
  • 1
    It's usually better to put things like "C#" into tags instead of the subject line. Commented Jan 8, 2011 at 17:13
  • Thanks, wouldn't search engines find it better though because it appears in a header tag? Commented Jan 8, 2011 at 17:16
  • 2
    I wouldn't worry about that too much. Search engines index SO pretty well. Let the SO team worry about optimizing that. :) Commented Jan 8, 2011 at 17:22
  • 2
    To be precise, you can't use such a syntax to initialize a List, use List<string> myString = new List<string> { "a", "b", "c", "d", "f" }; Commented Jan 8, 2011 at 17:25

4 Answers 4

6

Using Linq:

var result = 
    from a in myString
    from b in myString
    where a != b
    select a + b;
Sign up to request clarification or add additional context in comments.

Comments

4

Not using LINQ

List<string> myString = {"a", "b", "c", "d", "f"};
List<String> result = new List<String> ();
for (int i = 0; i < myString.Count; i++)
{
    for (int j = 0; j < myString.Count; j++)
    {
        if (i == j)
            continue;
        result.Add(myString[i] + myString[j]);
    }
}

1 Comment

second for loop should have j++ and not i++
1

Without LINQ, you can use a nested loop:

var permutations = new List<string>();
for(int i = 0; i < myString.Count; i++) 
{
    for(int j = 0; j < myString.Count; j++)
    {
        if(i == j)
            continue;

        var permutation = string.Format("{0}{1}", myString[i], myString[j]);
        permutations.Add(permutation);
    }
}

1 Comment

by having i < myString.Count - 1 you loop for one less element if I am not mistaken, you either need i <= myString.Count - 1 OR i < myString.Count
0

Complementing the previous answers:

        string[] arreglo = new string[6];
        arreglo[0] = "a";
        arreglo[1] = "b";
        arreglo[2] = "c";
        arreglo[3] = "d";
        arreglo[4] = "e";
        arreglo[5] = "f";

        var permutations = new List<string>();
        for (int i = 0; i < arreglo.Length; i++)
        {
            for (int j = 0; j < arreglo.Length; j++)
            {
                for (int k = 0; k < arreglo.Length; k++)
                {
                    for (int l = 0; l < arreglo.Length; l++)
                    {
                        for (int m = 0; m < arreglo.Length; m++)
                        {
                            for (int n = 0; n < arreglo.Length; n++)
                            {
                                if (i ==j ||j == k||i == k||k == l||i == l||j == l||i == m||j == m||k == m||l == m||i == n||j == n||k == n||l == n||m == n)
                                    continue;

                                var permutation = string.Format("{0}{1}{2}{3}{4}{5}", arreglo[i], arreglo[j], arreglo[k], arreglo[l], arreglo[m],arreglo[n]);
                                permutations.Add(permutation);
                            }
                        }
                    }
                }
            }
        }

        foreach(var element in permutations)
        {
            Console.WriteLine(element);
        }
        Console.ReadLine();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.