1

Ok, here is the problem that I'm having at the moment: I have a list of elements with which I want to make pairs of all possible combinations.

For example: I have a list of elements "A", "B", "C", "E".

Out of those elements I want to make pairs of all possible combinations (without duplicate elements that already exist while pairing elements), so it would become:

AB

AC

AE

BC

BE

CE

ABC

ABE

ACE

BCE

ABCE

So far, my code doesn't make all combinations like in the example above and it seems to be having a problem with duplicates and I've ran out of ideas how to approach this any further.

    List<char> charList = new List<char> { 'A', 'B', 'C', 'E' };
    List<string> copy = new List<string>();
    List<string> stringList = new List<string>();

    for (int i = 0; i < charList.Count() - 1; i++)
    {
        for (int j = i + 1; j < charList.Count(); j++)
        {
            stringList.Add(charList[i].ToString() + charList[j].ToString());
            copy = stringList.ToList();

        }
    }


    for (int i = 0; i < charList.Count() - 1; i++)
    {
        for (int j = i + 1; j < charList.Count(); j++)
        {
            for (int g = 0; g < stringList.Count(); g++)
            {

                if (!stringList[g].Contains(charList[i]))
                {
                    stringList[g] += charList[i];
                    copy.Add(stringList[g]);
                }
                else if (!stringList[g].Contains(charList[j]))
                {
                    stringList[g] += charList[j];
                    copy.Add(stringList[g]);
                }
            }

        }
    }


    foreach (string value in copy)
    {
        Console.WriteLine(value);
    }

Thanks.

  • Eric Lippert has a series about permutations, maybe this will help: http://ericlippert.com/2013/04/15/producing-permutations-part-one/ – germi Nov 21 '13 at 14:22
  • http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G – Tim Schmelter Nov 21 '13 at 14:23
  • Use this http://stackoverflow.com/questions/5128615/c-sharp-string-permutation – Oleh Nov 21 '13 at 14:30
  • possible duplicate of [Combination Generator in Linq](http://stackoverflow.com/questions/774457/combination-generator-in-linq) – Steve B Nov 21 '13 at 14:31

3 Answers3

1
var charList = new List<char> { 'A', 'B', 'C', 'E' };
List<string> stringList = new List<string>();

for (var i = 1; i < Math.Pow(2, charList.Count); i++)
{
    var sb = new StringBuilder();
    for (var j = 0; j < charList.Count; j++)
    {
        int power = (int)Math.Pow(2, j);
        if ((i & power) == power) sb.Append(charList[j]);
    }
    var s = sb.ToString();
    if (s.Length > 1) stringList.Add(sb.ToString());
}

// Sort results.
stringList.Sort((s1, s2) => s1.Length != s2.Length
    ? s1.Length.CompareTo(s2.Length) : s1.CompareTo(s2));
Vladimir
  • 7,345
  • 4
  • 34
  • 39
1

you can use library like this one on CodeProject: http://www.codeproject.com/Articles/26050/Permutations-Combinations-and-Variations-using-C-G

The code will look something like this:

for (int i = 1; i <= charList.Count(); i++)
{
  Combinations<char> combinations = new Combinations<char>(charList , i);
  foreach(IList<char> c in combinations)
  {
     Console.WriteLine("{{{0}}}", String.Join(" ", chars));
  }
}
avivr
  • 2,573
  • 3
  • 22
  • 34
0
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
        string[] list = new string[] { "A", "B", "C", "D" };
        List<string> combined = new List<string>();

        for (int i = 0; i < list.Length; i++)
        {
            combined.Add(list[i]);
            for (int j = 0; j < list.Length; j++)
            {
                combined.Add(list[i] + list[j]);
                for (int k = 0; k < list.Length; k++)
                {
                    combined.Add(list[i] + list[j] + list[k]);
                    for (int l = 0; l < list.Length; l++)
                    {
                        combined.Add(list[i]+list[j] + list[k] + list[l]);
                    }
                }
            }
        }

        combined.ForEach(item => Console.WriteLine(item));

        Console.ReadKey();
    }
}

}

Paul Sinnema
  • 2,534
  • 2
  • 20
  • 33