In this blog, we will learn, how to get all the combination of the elements in an array.Suppose, we have an integer array "myarrint", as given below.
- int[] myarrint = new[] { 1, 2, 3 };
Code:
- public int count = 0;
- protected void Page_Load(object sender, EventArgs e)
- {
- int[] myarrint = new[] { 1, 2, 3 };
- int[] responseint = Getdataint(myarrint);
- Response.Write(string.Join(",", responseint));
- }
- public int[] Getdataint(int[] arr)
- {
- count = arr.Length;
- return Combinationint(string.Join("", arr));
- }
- public int[] Combinationint(string str)
- {
- if (string.IsNullOrEmpty(str))
- throw new ArgumentException("Invalid input");
- if (str.Length == 1)
- return new int[] { Convert.ToInt32(str) };
- char c = str[str.Length - 1];
- //Recursive process starts here - Since return is an int array, below code will convert it into an string array for further processing
- string[] returnArray = Array.ConvertAll(Combinationint(str.Substring(0, str.Length - 1)), x => x.ToString());
- List<string> finalArray = new List<string>();
- foreach (string s in returnArray)
- finalArray.Add(s);
- finalArray.Add(c.ToString());
- int j = 0;
- foreach (string s in returnArray)
- {
- finalArray.Add(s + c);
- //If you don't need both 12 and 21 in result comment below line
- finalArray.Add(c + s);
- }
- //If you don't need both 12 and 21 in result comment below region
- #region sameelements
- returnArray = finalArray.ToArray();
- foreach (string s in returnArray)
- {
- if (str.Length == count)
- {
- if (s.Length < str.Length - 1)
- {
- foreach (char k in str)
- {
- foreach (char m in str)
- {
- if (k != m && !s.Contains(k) && !s.Contains(m))
- {
- finalArray.Add(s + k);
- finalArray.Add(s + m + k);
- finalArray.Add(m.ToString() + k.ToString());
- finalArray.Add(m + s + k);
- }
- }
- }
- }
- }
- j++;
- }
- #endregion
- //Converting the string array to int array
- int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
- //Sorting array
- Array.Sort(retarr);
- return retarr;
- }

Same method can be applied on the string arrays also, with slight changes in the code.
The response will is given below.

- public int count = 0;
- protected void Page_Load(object sender, EventArgs e)
- {
- string[] myarr = new[] { "a", "b", "c" };
- string[] response = Getdata(myarr);
- Response.Write(string.Join(",", response));
- }
- public string[] Getdata(string[] arr)
- {
- count = arr.Length;
- return Combination(string.Join("", arr));
- }
- public string[] Combination(string str)
- {
- if (string.IsNullOrEmpty(str))
- throw new ArgumentException("Invalid input");
- if (str.Length == 1)
- return new string[] { str };
- char c = str[str.Length - 1];
- //Recursive process starts here
- string[] returnArray = Combination(str.Substring(0, str.Length - 1));
- List<string> finalArray = new List<string>();
- foreach (string s in returnArray)
- finalArray.Add(s);
- finalArray.Add(c.ToString());
- int j = 0;
- foreach (string s in returnArray)
- {
- finalArray.Add(s + c);
- //If you don't need both 'ab' and 'ba' in result comment below line
- finalArray.Add(c + s);
- }
- //If you don't need both 'ab' and 'ba' in result comment below region
- #region sameelements
- returnArray = finalArray.ToArray();
- foreach (string s in returnArray)
- {
- if (str.Length == count)
- {
- if (s.Length < str.Length - 1)
- {
- foreach (char k in str)
- {
- foreach (char m in str)
- {
- if (k != m && !s.Contains(k) && !s.Contains(m))
- {
- finalArray.Add(s + k);
- finalArray.Add(s + m + k);
- finalArray.Add(m.ToString() + k.ToString());
- finalArray.Add(m + s + k);
- }
- }
- }
- }
- }
- j++;
- }
- #endregion
- //Sorting of array
- Array.Sort(finalArray.ToArray());
- return finalArray.Distinct().ToArray();
- }

If we don't need same combination, i.e for 12 and 21, we just need 12 and we can comment out certain codes(marked in red) in the function.
- public int[] Combinationint(string str)
- {
- if (string.IsNullOrEmpty(str))
- throw new ArgumentException("Invalid input");
- if (str.Length == 1)
- return new int[] { Convert.ToInt32(str) };
- char c = str[str.Length - 1];
- //Recursive process starts here - Since return is an int array, below code will convert it into an string array for further processing
- string[] returnArray = Array.ConvertAll(Combinationint(str.Substring(0, str.Length - 1)), x => x.ToString());
- List<string> finalArray = new List<string>();
- foreach (string s in returnArray)
- finalArray.Add(s);
- finalArray.Add(c.ToString());
- int j = 0;
- foreach (string s in returnArray)
- {
- finalArray.Add(s + c);
- //If you don't need both 12 and 21 in result comment below line
- //finalArray.Add(c + s);
- }
- //If you don't need both 12 and 21 in result comment below region
- //#region sameelements
- //returnArray = finalArray.ToArray();
- //foreach (string s in returnArray)
- //{
- // if (str.Length == count)
- // {
- // if (s.Length < str.Length - 1)
- // {
- // foreach (char k in str)
- // {
- // foreach (char m in str)
- // {
- // if (k != m && !s.Contains(k) && !s.Contains(m))
- // {
- // finalArray.Add(s + k);
- // finalArray.Add(s + m + k);
- // finalArray.Add(m.ToString() + k.ToString());
- // finalArray.Add(m + s + k);
- // }
- // }
- // }
- // }
- // }
- // j++;
- //}
- //#endregion
- //Converting the string array to int array
- int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
- //Sorting array
- Array.Sort(retarr);
- return retarr;
- }
The output will be 1,2,3,12,13,23,123
Since this code is using recursion, the performance may be affected for very large arrays. Hope, this will be helpful for someone out there.

arunkumar peyalaPosted Sep 9, 2022, 7:04 AM
Bro can you slove this problem plsEx. 1:Number: 7591 Commas: 1 So you can place comma after 7 or after 5 or after 9, possible combinations would be, 7 and 591 75 and 91 759 and 1
Eric ConklinPosted Jun 26, 2020, 7:21 AM
This would be a permutation, not a combination.
Midhun TpPosted Sep 12, 2016, 4:21 AM
Thanks Bikesh Srivastava
Bikesh SrivastavaPosted Sep 12, 2016, 3:57 AM
Nice blog Midhun T P