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
Super Contributor

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, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal
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]]

Super Contributor

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. Smiley Happy 


Click "Accept as Solution" if my answer has helped, and remember to give "kudos" Smiley Happy

Thanks and Regards,
Himanshu Tayal