cancel
Showing results for 
Search instead for 
Did you mean: 

How to print all possible combinations of a given array

Highlighted
New Contributor

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 3
Highlighted
Community Leader

Re: How to print all possible combinations of a given array

@akashreuben7 ,

 

Hope below link will help

 

print-all-possible-combinations-of-r-elements-in-a-given-array-of-size-n

 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
Highlighted
New Contributor

Re: How to print all possible combinations of a given array

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]]

Highlighted
Community Leader

Re: How to print all possible combinations of a given array

Hi @akashreuben7 ,

 

You can accept it as solution, so that it will help others seking for help. 🙂 


Click "Accept as Solution" if my answer has helped,
Remember to give "Kudos" 🙂 ↓↓↓↓↓



Thanks and Regards,
Himanshu Tayal
New Here?
Join us and watch the welcome video:
Top Kudoed Authors