Could you please tell me if there are any built-in functions or LINQ functions that allow you to get all possible combinations of elements in a list?
That is, if I have a list of {bread, milk, chips, mustard}
get combinations
{(bread), (milk), (chips), (mustard), (bread milk), (bread chips), (bread mustard), (milk chips), (milk mustard), (chips mustard), (bread milk chips), (bread milk mustard), (bread chips mustard), (milk chips mustard), (bread milk chips mustard)
}.
A one-dimensional list is input to the function, and the result is a two-dimensional one.
If there are no built-in ones, have any ideas how to implement this? There will be such combinations
2 ^ (number of items in the list) - 1
.
I have already managed to implement 2-element combinations, but in this case, I have no ideas yet.
Here’s an example for 2 element combinations:
for (int i = 0; i & lt; prevItemSet.Count - 1; i ++)
{
for (int j = i + 1; j & lt; prevItemSet.Count; j ++)
{
if (! ItemSetList [i] .Equals (ItemSetList [j]))
{
bool isSub = false;
for (int l = 0; l & lt; k - 1; l ++)
{
if (ItemSetList [i] [l] == ItemSetList [j] [l])
isSub = true;
else
{
isSub = false;
break;
}
}
if (isSub)
{
if (ItemSetList [i] .Count == ItemSetList [j] .Count)
{
var tempList = CombineItems (ItemSetList [i], ItemSetList [j]);
int supp = SupportCount (tempList);
if (! (tempList.Count == 0))
nextCandidate.Add (tempList, supp);
}
}
// var tempList = CombineItems (ItemSetList [i], ItemSetList [j]);
// int supp = SupportCount (tempList);
// if (! (tempList.Count == 0))
// nextCandidate.Add (tempList, supp);
}
else continue;
}
}
public List & lt; string & gt; CombineItems (List & lt; string & gt; firstList, List & lt; string & gt;
secondList)
{
List & lt; string & gt; resList = new List & lt; string & gt; ();
int k = firstList.Count; // number of elements in the set
resList = firstList;
resList.Add (secondList [k - 1]);
// for (int i = 0; i & lt; k - 1; i ++)
// {
// if (firstList [i] == secondList [i])
// resList.Add (firstList [i]);
//}
// if (resList.Count == k - 1)
// resList.Add (secondList [k - 1]);
return resList;
}
Answer 1, authority 100%
I think something like this (explanations in the code)
public static class Helper
{
/// & lt; summary & gt;
/// Returns a list of lists of elements of the passed list without repetitions
/// & lt; / summary & gt;
/// & lt; typeparam name = "T" & gt; Item type & lt; / typeparam & gt;
/// & lt; param name = "list" & gt; The original list & lt; / param & gt;
/// & lt; returns & gt; & lt; / returns & gt;
public static IList & lt; IList & lt; T & gt; & gt; GetAllOptionsWithoutRepetition & lt; T & gt; (this List & lt; T & gt; list)
{
// for storing binary numbers
var templates = new List & lt; string & gt; ();
// count how many numbers will be in the answer and translate them into binary, starting from 0
for (int i = 0; i & lt; Math.Pow (2, list.Count); i ++)
{
var bin = Convert.ToString (i, 2);
// also padding with zeros in front for completeness of the binary number
templates.add ($ "{" 0 ".repeat (List.Count - bin.length)} {bin}");
}
// Future result
var risall = new list & lt; ilist & lt; t & gt; & gt; ();
Foreach (Var Template in Templates)
{
VAR Res = New List & lt; T & GT; ();
for (int ch = 0; ch & lt; template.length; ch ++)
{
// If something must stand in this place, I put (in accordance with the position in the incoming array)
if (Template [CH] == '1') res.add (list [ch]);
}
// If the array is not empty, I add to the main result
if (res.Count & gt; 0) Resall.add (RES);
}
// Returning Reuses, sterning him to
Return Resall.orderby (s = & gt; s.count) .TheNBY (S = & gt; string.join (", s)). Tolist ();
}
/// & LT; Summary & GT;
/// Returns n times the same string
/// & LT; / Summary & GT;
/// & lt; param name = "str" & gt; string & lt; / param & gt;
/// & lt; param name = "num" & gt; number of repetitions & lt; / param & gt;
/// & LT; Returns & gt; & lt; / returns & gt;
Public Stator String Repeat (This String Str, Int Num)
{
String Result = string.empty;
for (int i = 0; i & lt; num; i ++)
{
result + = str;
}
RETURN RESULT;
}
}
Test:
list & lt; string & gt; L = New List & LT; String & GT; {"A", "b", "c"};
var test = l.getalloptionswithoutrepetition ();
Foreach (Var Strs in Test)
{
Console.WriteLine (String.join (",", StRS));
}
Result:
Your Test:
list & lt; string & gt; L = New List & LT; String & GT; {"Bread", "Milk", "Chips", "Mustard"};
var test = l.getalloptionswithoutrepetition ();
Foreach (Var Strs in Test)
{
Console.WriteLine (String.join (",", StRS));
}
Conclusion: