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.
  1. 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:
  1. public int count = 0;
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. int[] myarrint = new[] { 1, 2, 3 };
  5. int[] responseint = Getdataint(myarrint);
  6. Response.Write(string.Join(",", responseint));
  7. }
  8. public int[] Getdataint(int[] arr)
  9. {
  10. count = arr.Length;
  11. return Combinationint(string.Join("", arr));
  12. }
  13. public int[] Combinationint(string str)
  14. {
  15. if (string.IsNullOrEmpty(str))
  16. throw new ArgumentException("Invalid input");
  17. if (str.Length == 1)
  18. return new int[] { Convert.ToInt32(str) };
  19. char c = str[str.Length - 1];
  20. //Recursive process starts here - Since return is an int array, below code will convert it into an string array for further processing
  21. string[] returnArray = Array.ConvertAll(Combinationint(str.Substring(0, str.Length - 1)), x => x.ToString());
  22. List<string> finalArray = new List<string>();
  23. foreach (string s in returnArray)
  24. finalArray.Add(s);
  25. finalArray.Add(c.ToString());
  26. int j = 0;
  27. foreach (string s in returnArray)
  28. {
  29. finalArray.Add(s + c);
  30. //If you don't need both 12 and 21 in result comment below line
  31. finalArray.Add(c + s);
  32. }
  33. //If you don't need both 12 and 21 in result comment below region
  34. #region sameelements
  35. returnArray = finalArray.ToArray();
  36. foreach (string s in returnArray)
  37. {
  38. if (str.Length == count)
  39. {
  40. if (s.Length < str.Length - 1)
  41. {
  42. foreach (char k in str)
  43. {
  44. foreach (char m in str)
  45. {
  46. if (k != m && !s.Contains(k) && !s.Contains(m))
  47. {
  48. finalArray.Add(s + k);
  49. finalArray.Add(s + m + k);
  50. finalArray.Add(m.ToString() + k.ToString());
  51. finalArray.Add(m + s + k);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. j++;
  58. }
  59. #endregion
  60. //Converting the string array to int array
  61. int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
  62. //Sorting array
  63. Array.Sort(retarr);
  64. return retarr;
  65. }
The response is given below.

Same method can be applied on the string arrays also, with slight changes in the code.
  1. public int count = 0;
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. string[] myarr = new[] { "a", "b", "c" };
  5. string[] response = Getdata(myarr);
  6. Response.Write(string.Join(",", response));
  7. }
  8. public string[] Getdata(string[] arr)
  9. {
  10. count = arr.Length;
  11. return Combination(string.Join("", arr));
  12. }
  13. public string[] Combination(string str)
  14. {
  15. if (string.IsNullOrEmpty(str))
  16. throw new ArgumentException("Invalid input");
  17. if (str.Length == 1)
  18. return new string[] { str };
  19. char c = str[str.Length - 1];
  20. //Recursive process starts here
  21. string[] returnArray = Combination(str.Substring(0, str.Length - 1));
  22. List<string> finalArray = new List<string>();
  23. foreach (string s in returnArray)
  24. finalArray.Add(s);
  25. finalArray.Add(c.ToString());
  26. int j = 0;
  27. foreach (string s in returnArray)
  28. {
  29. finalArray.Add(s + c);
  30. //If you don't need both 'ab' and 'ba' in result comment below line
  31. finalArray.Add(c + s);
  32. }
  33. //If you don't need both 'ab' and 'ba' in result comment below region
  34. #region sameelements
  35. returnArray = finalArray.ToArray();
  36. foreach (string s in returnArray)
  37. {
  38. if (str.Length == count)
  39. {
  40. if (s.Length < str.Length - 1)
  41. {
  42. foreach (char k in str)
  43. {
  44. foreach (char m in str)
  45. {
  46. if (k != m && !s.Contains(k) && !s.Contains(m))
  47. {
  48. finalArray.Add(s + k);
  49. finalArray.Add(s + m + k);
  50. finalArray.Add(m.ToString() + k.ToString());
  51. finalArray.Add(m + s + k);
  52. }
  53. }
  54. }
  55. }
  56. }
  57. j++;
  58. }
  59. #endregion
  60. //Sorting of array
  61. Array.Sort(finalArray.ToArray());
  62. return finalArray.Distinct().ToArray();
  63. }
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.
  1. public int[] Combinationint(string str)
  2. {
  3. if (string.IsNullOrEmpty(str))
  4. throw new ArgumentException("Invalid input");
  5. if (str.Length == 1)
  6. return new int[] { Convert.ToInt32(str) };
  7. char c = str[str.Length - 1];
  8. //Recursive process starts here - Since return is an int array, below code will convert it into an string array for further processing
  9. string[] returnArray = Array.ConvertAll(Combinationint(str.Substring(0, str.Length - 1)), x => x.ToString());
  10. List<string> finalArray = new List<string>();
  11. foreach (string s in returnArray)
  12. finalArray.Add(s);
  13. finalArray.Add(c.ToString());
  14. int j = 0;
  15. foreach (string s in returnArray)
  16. {
  17. finalArray.Add(s + c);
  18. //If you don't need both 12 and 21 in result comment below line
  19. //finalArray.Add(c + s);
  20. }
  21. //If you don't need both 12 and 21 in result comment below region
  22. //#region sameelements
  23. //returnArray = finalArray.ToArray();
  24. //foreach (string s in returnArray)
  25. //{
  26. // if (str.Length == count)
  27. // {
  28. // if (s.Length < str.Length - 1)
  29. // {
  30. // foreach (char k in str)
  31. // {
  32. // foreach (char m in str)
  33. // {
  34. // if (k != m && !s.Contains(k) && !s.Contains(m))
  35. // {
  36. // finalArray.Add(s + k);
  37. // finalArray.Add(s + m + k);
  38. // finalArray.Add(m.ToString() + k.ToString());
  39. // finalArray.Add(m + s + k);
  40. // }
  41. // }
  42. // }
  43. // }
  44. // }
  45. // j++;
  46. //}
  47. //#endregion
  48. //Converting the string array to int array
  49. int[] retarr = (Array.ConvertAll(finalArray.ToArray(), int.Parse)).Distinct().ToArray();
  50. //Sorting array
  51. Array.Sort(retarr);
  52. return retarr;
  53. }
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.