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 };
We need to get all the combination of elements in an array without repeating it. This will be 1,2,3,12,13,21,23,31,32,123,132,213,231,312,321.
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];
-
- 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);
-
- finalArray.Add(c + s);
- }
-
- #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
-
- int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
-
- Array.Sort(retarr);
- return retarr;
- }
The response is given below.
![]()
Same method can be applied on the string arrays also, with slight changes in the code.
- 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];
-
- 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);
-
- finalArray.Add(c + s);
- }
-
- #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
-
- Array.Sort(finalArray.ToArray());
- return finalArray.Distinct().ToArray();
- }
The response will is given below.
![]()
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];
-
- 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);
-
-
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
-
- 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.