Forum Discussion

akashreuben7's avatar
akashreuben7
New Contributor
5 years ago

How to print all possible combinations of a given array

Hello Team I have an array of 

def Z = ['a','b','c','d','e','f','g']

how to get all possible 3 alphabet combinations without each element repeat by itself. 

Output required: [a.b.c],[a,b,d] and so on but shouldnt have [a,a,a] or [a,a,b] something like this

 

3 Replies

    • akashreuben7's avatar
      akashreuben7
      New Contributor

      Awesome this helps himanshu.

       

      I have found another way to do it in a simple format.

      def Z = ['a','b','c','d','e']

      def result =[Z,Z,Z].combinations().findAll{x,y,z -> x<y&y<z}

      def result2 = [Z,Z].combinations().findAll{x,y -> x<y}

      def finalresult = result + result2
      log.info finalresult

       

      result :[[a, b, c], [a, b, d], [a, c, d], [b, c, d], [a, b, e], [a, c, e], [b, c, e], [a, d, e], [b, d, e], [c, d, e], [a, b], [a, c], [b, c], [a, d], [b, d], [c, d], [a, e], [b, e], [c, e], [d, e]]