none
Permutations for an integer array RRS feed

  • Question

  • I have an integer array of length N, I need to get all the 
    possible permutations of the elements in the array.
    for example:
    I have an array of N =3, ie., int[] array ={1, 2, 3}.
    Total no of ways 3! =6, so I will get 6 possible ways
    {1, 2, 3}
    {2, 1, 3}
    {2, 3, 1}
    {3, 2, 1}
    {3, 1, 2}
    {1, 3, 2}
    Note: The output need to be integer array so that I can pass this array to some other method..
    Thanks in advance..

    Basha Shaik

    Sunday, November 23, 2014 5:42 AM

Answers

  • Hello Shaik,

    According to your description, there is an example which could achieve your requirement, please check it below:

    class Program
    
        { 
    
            static void Main(string[] args)
    
            {
    
                try
    
                {
    
                    #region https://social.msdn.microsoft.com/Forums/en-US/43f7b574-6426-41b2-9f54-9256e44cbb6b/permutations-for-an-integer-array?forum=netfxbcl
    
                    List<int> lists = new List<int>() { 1, 2, 3 };
    
                    List<List<int>> resultList = new List<List<int>>();
    
                    //Get All Combinations
    
                    var result = GetCombinations(lists, lists.Count).ToList();
    
                    foreach (var item in result)
    
                    {
    
                        if (item.GroupBy(i => i).Count() == lists.Count)
    
                        {
    
                            resultList.Add(item.ToList());
    
                        }
    
                    }
    
                    #endregion
    
                }
    
                catch (Exception e)
    
                {
    
                    Console.WriteLine("An exception occurred.");
    
                    Console.WriteLine("Source: {0}", e.Source);
    
                    Console.WriteLine("Message: {0}", e.Message);
    
                }
    
            }
    
            static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length)
    
            {
    
                if (length == 1) return list.Select(t => new T[] { t });
    
    
                return GetCombinations(list, length - 1)
    
                    .SelectMany(t => list, (t1, t2) => t1.Concat(new T[] { t2 }));
    
            }
    
        }
    

    The result would be a collection contains permutations for the given integer array

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 24, 2014 4:38 AM
    Moderator

All replies

  • Hello Shaik,

    According to your description, there is an example which could achieve your requirement, please check it below:

    class Program
    
        { 
    
            static void Main(string[] args)
    
            {
    
                try
    
                {
    
                    #region https://social.msdn.microsoft.com/Forums/en-US/43f7b574-6426-41b2-9f54-9256e44cbb6b/permutations-for-an-integer-array?forum=netfxbcl
    
                    List<int> lists = new List<int>() { 1, 2, 3 };
    
                    List<List<int>> resultList = new List<List<int>>();
    
                    //Get All Combinations
    
                    var result = GetCombinations(lists, lists.Count).ToList();
    
                    foreach (var item in result)
    
                    {
    
                        if (item.GroupBy(i => i).Count() == lists.Count)
    
                        {
    
                            resultList.Add(item.ToList());
    
                        }
    
                    }
    
                    #endregion
    
                }
    
                catch (Exception e)
    
                {
    
                    Console.WriteLine("An exception occurred.");
    
                    Console.WriteLine("Source: {0}", e.Source);
    
                    Console.WriteLine("Message: {0}", e.Message);
    
                }
    
            }
    
            static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length)
    
            {
    
                if (length == 1) return list.Select(t => new T[] { t });
    
    
                return GetCombinations(list, length - 1)
    
                    .SelectMany(t => list, (t1, t2) => t1.Concat(new T[] { t2 }));
    
            }
    
        }
    

    The result would be a collection contains permutations for the given integer array

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, November 24, 2014 4:38 AM
    Moderator
  • what is your problem here, you want to make n! - 1 arrays from an available integer array of length n ? if it is true, it can take a lot of time for computer to calculate when n is a big number, can you give us your n.
    Monday, November 24, 2014 7:02 AM